PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA、最低公共祖先
文章目录
- 题目分析
- 题目链接
题目分析
来源:acwing
分析:二叉搜索树的中序遍历是隐含给定的,它的中序遍历就是从小到大排列。
所以这道题先是根据给定的前序遍历和中序遍历,建树。
建树的时候需要用到点的深度和每个点的父节点是什么。
然后是找LCA,有很多优秀的算法,这里使用朴素做法,O(n)复杂度。思想是:两个结点a和b,深度深的往上等于它的父节点;如果两者位于同一层,任意一个往上走等于它的父亲,直到两者相等。相等时就是最低公共祖先。
while( a != b){if(depth[a] < depth[b]) b =p[b];else a = p[a];
}
这里进行了映射(离散化,加快查找速度,或者防止爆掉),中序序列映射为0~n-1
;先序遍历根据中序遍历映射成0~n-1内的下标,同样一一对应。
ac代码
#include<bits/stdc++.h>
using namespace std;
const int N = 10010;
int n ,m;
int in[N],pre[N];//存的是离散化之后的值
int seq[N];//保存原数组,凭借离散化的值可以找到原值
unordered_map<int,int> pos;
int p[N],depth[N];//根据中序遍历和前序遍历建树
int build( int il ,int ir ,int pl ,int pr ,int d){int root = pre[pl];int k = root;depth [root] = d;if(il < k) p[build(il , k -1 , pl+1, pl+1 + k -1 -il , d+1)] = root;if( k< ir) p[build(k+1,ir ,pl+ k - il+1, pr,d+1)] =root ;return root;
}int main(){cin>> m >>n;//读入前序序列for(int i = 0; i< n; i++){cin >> pre[i];seq[i] = pre[i];in[i] =i; //中序遍历离散化0~n-1}sort(seq,seq+n);//先排序之后是中序遍历for(int i = 0; i< n; i++) pos[seq[i]] = i; //中序序列映射到0到n-1for(int i = 0; i < n; i++) pre[i] =pos[pre[i]]; //前序遍历离散化0~n-·,借助posbuild(0,n-1,0,n-1, 0);//查询while(m--){int a ,b;cin >> a >> b;//找到LCAif(pos.count(a) && pos.count(b)){a = pos[a] ,b= pos[b];int x =a, y =b;while( a != b){if(depth[a] < depth[b]) b =p[b];else a = p[a];}if( a != x && a != y) printf("LCA of %d and %d is %d.\n",seq[x],seq[y],seq[a]);else if(a == x ) printf("%d is an ancestor of %d.\n",seq[x],seq[y]);else printf("%d is an ancestor of %d.\n",seq[y],seq[x]);}else if(pos.count(a) ==0 && pos.count(b)==0) printf("ERROR: %d and %d are not found.\n",a,b);else if(pos.count(a) ==0)printf("ERROR: %d is not found.\n",a);else printf("ERROR: %d is not found.\n",b);}}
题目链接
PAT甲级1143 Lowest Common Ancestor (30 分)
https://www.acwing.com/problem/content/1638/
PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA、最低公共祖先相关推荐
- 【题解】【PAT甲】1143 Lowest Common Ancestor (30 分)(树)(BST)(最近公共祖先)
题目链接 PTA | 程序设计类实验辅助教学平台 题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the ...
- 1143 Lowest Common Ancestor (30 分)【难度: 中 / 知识点: 最低公共祖先 未完成】
https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312
- 1143. Lowest Common Ancestor
1143. Lowest Common Ancestor (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT甲级1076 Forwards on Weibo (30 分) :[C++题解]图论、bfs
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: BFS如何搜前k层?统计前k层的点数. ac代码 #include<bits/stdc++.h> using names ...
- PAT甲级1068 Find More Coins (30 分):[C++题解]DP、背包问题、dp输出方案
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:m是背包容量,a1,a2,....,ana_1,a_2,....,a_na1,a2,....,an是n个物品,第i个物品的体积是 ...
- PAT甲级1045 Favorite Color Stripe (30 分):[C++题解]最佳彩色带、DP、公共子序列变形
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:这是一个公共子序列的问题.但是有点变式,序列a和序列b不是完全等价的,序列a的每个元素可以对应多个相同元素,而且有些元素可以不使用.比 ...
- PAT甲级1119 Pre- and Post-order Traversals (30分):[C++题解]暴搜dfs、前序遍历和后序遍历求中序遍历
文章目录 题目分析 题目链接 题目分析 分析 给了前序遍历和后序遍历,能够确定根结点,但是左子树和右子树的长度是不确定的.这里采用的解决方案是枚举左子树的结点个数,其实右子树的结点个数也确定了.对于每 ...
- PAT甲级-1045 Favorite Color Stripe (30分)
点击链接PAT甲级-AC全解汇总 题目: Eva is trying to make her own color stripe out of a given one. She would like t ...
- LeetCode Lowest Common Ancestor of a Binary Tree(LCA问题)
问题:求二叉树中两个结点p,q的最近公共祖先 思路:第一种方法是二叉树的递归,当搜索是当前结点为p或者为q时,直接返回对应结点.然后再左右子树的返回情况 1.如果左右子树非空,则当前结点就是要找的最近 ...
最新文章
- 经典网络AlexNet介绍
- -bash:XXX: command not found报错(云服务centos)
- Qt Creator编辑状态图
- 利用反射操作bean的属性和方法
- 互联网赚钱,必须聚焦这三件事
- [原][osgearth]osgearthviewer读取earth文件,代码解析(earth文件读取的一帧)
- c++之求数组的最大最小值及其下标
- scipy.special —— 排列、组合与阶乘
- 峰Spring4学习(5)bean之间的关系和bean的作用范围
- Centos网络管理(五)-Bonding、网络组和网桥
- socket工作原理深入分析
- c语言中用什么表示空串,C语言怎么表示空串
- 跨境电商平台哪个好?——扬帆际海
- win10计算机亮度在哪里调,win10电脑怎么调亮度
- 【GameMaker】加速Runtime下载
- 2021赤峰二中高考成绩查询,2021年赤峰高考状元名单公布,赤峰高考状元学校资料及最高分...
- Code39码如何批量生成
- vue2.x的h函数(createElement)与vue3中的h函数
- jis计算机基础知识讲课,计算机基础知识——中文输入法教学教案.ppt
- html标签之列表标签,前端开发资料分享