The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.
Given any two nodes in a binary tree, you are supposed to find their LCA.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.
Output Specification:
For each given pair of U and V, print in a line LCA of U and V is A.
if the LCA is found and A
is the key. But if A
is one of U and V, print X is an ancestor of Y.
where X
is A
and Y
is the other node. If U or V is not found in the binary tree, print in a line ERROR: U is not found.
or ERROR: V is not found.
or ERROR: U and V are not found.
.
Sample Input:
6 8
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99
Sample Output:
LCA of 2 and 6 is 3.
8 is an ancestor of 1.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.
#include<bits/stdc++.h>
using namespace std;
#define bug(x) printf("x:%d****\n",x)
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
const int N=20010;
map<int,int> id;
vector<int>vec[N];
int lmr[N],mlr[N];
int n,m;
void build(int* mlr,int* lmr,int len,int f){
//printf("f:%d len:%d\n",f,len);
if(len==0)return;
int x=mlr[0];
vec[f].push_back(x);vec[x].push_back(f);
//if(len==1)return;
int i;
for(i=0;i<len;i++){
if(lmr[i]==x)break;
}
build(mlr+1,lmr,i,x);
build(mlr+i+1,lmr+i+1,len-i-1,x);
}
int seq[N*2],R[N*2],in[N],dep[N*2],tot=0;
void dfs(int x,int f){
//printf("x:%d f:%d\n",x,f);
seq[++tot]=x;
R[tot]=dep[x];
in[x]=tot;
int sz=vec[x].size();
for(int i=0;i<sz;i++){
int v=vec[x][i];
if(v==f)continue;
dep[v]=dep[x]+1;
dfs(v,x);
seq[++tot]=x;
R[tot]=dep[x];
}
}
int Log[N],dp[N][20];
void RMQ(int n){
int lm=Log[n];
for(int i=0;i<=n;i++)dp[i][0]=i;
for(int j=1;j<=Log[n];j++){
for(int i=0;i+(1<<j)-1<=n;i++){
int x1=dp[i][j-1],x2=dp[i+(1<<(j-1))][j-1];
dp[i][j]=R[x1]<R[x2]?x1:x2;
}
}
}
int LCA(int x,int y){
x=in[x],y=in[y];
if(x>y)swap(x,y);
int len=y-x+1;
int t=Log[len];
int u=dp[x][t],v=dp[y-(1<<t)+1][t];
return R[u]<R[v]?seq[u]:seq[v];
}
int num[N];
int main()
{
Log[1]=0;for(int i=2;i<N;i++)Log[i]=Log[i/2]+1;
//rep(i,1,20)printf("i:%d %d\n",i,Log[i]);
scanf("%d %d",&m,&n);
for(int i=1;i<=n;i++){
scanf("%d",&lmr[i]);
num[i]=lmr[i];
}
for(int i=1;i<=n;i++){
scanf("%d",&mlr[i]);
}
sort(num+1,num+n+1);
rep(i,1,n+1)id[num[i]]=i;
rep(i,1,n+1){
lmr[i]=id[lmr[i]];
mlr[i]=id[mlr[i]];
}
build(mlr+1,lmr+1,n,0);
//bug(1);
dfs(mlr[1],-1);
RMQ(tot);
rep(i,0,m){
int u,v;
scanf("%d %d",&u,&v);
int f1=0,f2=0;
if(id[u])f1=1;
if(id[v])f2=1;
if(!f1&&!f2){
printf("ERROR: %d and %d are not found.\n",u,v);
}else if(!f1||!f2){
printf("ERROR: %d is not found.\n",f1?v:u);
}else{
int uu=id[u],vv=id[v];
int x=LCA(uu,vv);
//bug(x);bug(uu);bug(vv);
if(x==uu||x==vv)
printf("%d is an ancestor of %d.\n",num[x],num[uu+vv-x]);
else
printf("LCA of %d and %d is %d.\n",u,v,num[x]);
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define bug(x) printf("x:%d****\n",x)
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
const int N=20010;
map<int,int> id;
vector<int>vec[N];
int lmr[N],mlr[N];
int n,m;
void build(int* mlr,int* lmr,int len,int f){
//printf("f:%d len:%d\n",f,len);
if(len==0)return;
int x=mlr[0];
vec[f].push_back(x);vec[x].push_back(f);
//if(len==1)return;
int i;
for(i=0;i<len;i++){
if(lmr[i]==x)break;
}
build(mlr+1,lmr,i,x);
build(mlr+i+1,lmr+i+1,len-i-1,x);
}
int dp[N][20],depth[N];
void dfs(int x,int fa){
//printf("x:%d fa:%d\n",x,fa);
dp[x][0]=fa;
int sz=vec[x].size();
for(int i=0;i<sz;i++){
int v=vec[x][i];
if(v==fa)continue;
depth[v]=depth[x]+1;
dfs(v,x);
}
}
int max_h=0;
void RMQ(int n){
while((1<<max_h)<n)max_h++;
for(int j=1;j<=max_h;j++){
for(int i=1;i<=n;i++){
dp[i][j]=dp[dp[i][j-1]][j-1];
}
}
}
int LCA(int x,int y){
if(depth[x]>depth[y])swap(x,y);
int t=depth[y]-depth[x];
for(int i=0;i<=max_h;i++){
if(t&(1<<i)){
y=dp[y][i];
}
}
if(y==x)return x;
//printf("x:%d y:%d\n",x,y);
for(int i=max_h;i>=0;i--){
if(dp[x][i]!=dp[y][i]){
x=dp[x][i];
y=dp[y][i];
}
}
//printf("xx:%d yy:%d\n",x,y);
return dp[x][0];
}
int num[N];
int main()
{
//Log[1]=0;for(int i=2;i<N;i++)Log[i]=Log[i/2]+1;
//rep(i,1,20)printf("i:%d %d\n",i,Log[i]);
scanf("%d %d",&m,&n);
for(int i=1;i<=n;i++){
scanf("%d",&lmr[i]);
num[i]=lmr[i];
}
for(int i=1;i<=n;i++){
scanf("%d",&mlr[i]);
}
sort(num+1,num+n+1);
rep(i,1,n+1)id[num[i]]=i;
rep(i,1,n+1){
lmr[i]=id[lmr[i]];
mlr[i]=id[mlr[i]];
}
//bug(1);
build(mlr+1,lmr+1,n,0);
// bug(2);
dfs(mlr[1],0);
//bug(3);
RMQ(n);
rep(i,0,m){
int u,v;
scanf("%d %d",&u,&v);
int f1=0,f2=0;
if(id[u])f1=1;
if(id[v])f2=1;
if(!f1&&!f2){
printf("ERROR: %d and %d are not found.\n",u,v);
}else if(!f1||!f2){
printf("ERROR: %d is not found.\n",f1?v:u);
}else{
int uu=id[u],vv=id[v];
int x=LCA(uu,vv);
//bug(x);bug(uu);bug(vv);
if(x==uu||x==vv)
printf("%d is an ancestor of %d.\n",num[x],num[uu+vv-x]);
else
printf("LCA of %d and %d is %d.\n",u,v,num[x]);
}
}
return 0;
}
最近公共祖先有两种表达方式
1. RMQ方式来实现。
考虑树的欧拉序,也就是dfs序,两个点的最近公共祖先肯定在这中间,所以我们只需要知道在这两个点中间的区间内
深度值最小的是谁,就好了。
主要步骤:
(1)建图,也就是找到所有的邻接边。
(2)dfs (祖先结点一般是0)找到所有结点的父亲结点
(3) 构造ST表
(4)查询ST表。 注意要 in[a]<in[b]
2.树形dp
核心还是一种递推关系,通过dfs我们可以直接找到他们的父亲是谁,然后利用这种传递性,我们可以知道他的的祖先是谁。 当询问两个点的最近公共祖先的时候,我们先跳到同一水平线上,然后看看是不是
是同一个祖先,如果是的话,就不动,如果不是的话,我们就动,所以肯定可以到 最近公共祖先的子节点上,因为这个距离肯定可以用二进制表达出来,所以我们就得到了结果。
主要步骤:
(1)建图,找到所有的邻接边
(2)dfs 找到所有结点的父节点,深度值
(3) 构造表
(4)查询表。
树的遍历方式的转换:
MLR 和 LMQ 构造出来树。
考虑 MLR肯定能找到父节点, 所以肯定能在第一个序列中区分出来左右子树。 但是在往下递归,就不是很好搞了,因为我们没有办法 确定一棵树。 但是我们知道一棵树上的结点肯定是相邻的,所以我们只要知道 这棵树的长度和起始位置就可以。
一开始肯定是一棵树, 所以起始位置和长度是 很容易的, 然后在LMR中找到根,我们就可以知道左子树的长度了,同理我们就又可以找到 MLR中左子树的长度。 所以就又可以递归,直到结束。
所以我们发现 MLR提供了根节点, 而LMR 提供了子树的长度,所以结合起来,我们就可以知道树的范围了。