Recent Common Ancestor (LCA)
As the name suggests, it is the most recent common parent node two nodes;
Here the use of the doubling of thinking, when you want one by one up incrementally, high complexity, increasing the number to be put into a binary representation (because any number can be obtained by adding a binary number);
11 for example, may be made of 2 ^ 3 = 8, 2 ^ 1 = 2, 2 ^ 1 = 0, the addition is made; this number may be 11 so that the first plus 8, plus the 2, obtained plus 1;
There are two pre-treatment, was a number of layers where each node, a node was to go up each node j th power of 2. obtained;
These two operations can be placed inside a dfs function, it should be very easy to understand:
fa [sn] [i] = fa [fa [sn] [i-1]] [i-1]; analog manually, meaning approximately 2 ^ n = 2 ^ n-1 * 2 ^ n-1;
void dfs(int sn,int ft){
dep[sn]=dep[ft]+1,fa[sn][0]=ft;
for(int i=1;i<=lg[dep[sn]];i++) fa[sn][i]=fa[fa[sn][i-1]][i-1];
for(int i=head[sn];~i;i=edge[i].nex){
if(edge[i].to!=ft) dfs(edge[i].to,sn);
}
}
Here also learned Preconditioned log_2 (i) +1 method:
for(int i=1;i<=n;i++) lg[i]=lg[i-1]+(1<<lg[i-1]==i);//预处理(log_2(i))+1的值
This thing is seeking to use the maximum number of times a binary number consisting of:
11 For example, it lg [11] = 4, we call lg [11] even when minus 1, that is to make up 11, up to as long as three times;
Then the main idea of the code is the core of lca: first the x and y at the same level, then go up together, findTopmostThe two parent nodes x and y, the nodes x and y are equal, then the parent node x is the answer;
int lca(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
while(dep[x]>dep[y]) x=fa[x][lg[dep[x]-dep[y]]-1];
if(x==y) return x;
for(int k=lg[dep[x]]-1;k>=0;k--){
if(fa[x][k]!=fa[y][k]){
x=fa[x][k],y=fa[y][k];
}
}
return fa[x][0];
}
This question is do all topics:
[template] recent common ancestor (LCA)
All Code:
#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=500100;
const int M=1000100;
const LL mod=100000000;
int n,m,s,head[N],cnt,lg[N],dep[N],fa[N][40];
struct Node{
int to,nex;
}edge[M];
void add(int p,int q){
edge[cnt].to=q;
edge[cnt].nex=head[p];
head[p]=cnt++;
}
void dfs(int sn,int ft){
dep[sn]=dep[ft]+1,fa[sn][0]=ft;
for(int i=1;i<=lg[dep[sn]];i++) fa[sn][i]=fa[fa[sn][i-1]][i-1];
for(int i=head[sn];~i;i=edge[i].nex){
if(edge[i].to!=ft) dfs(edge[i].to,sn);
}
}
int lca(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
while(dep[x]>dep[y]) x=fa[x][lg[dep[x]-dep[y]]-1];
if(x==y) return x;
for(int k=lg[dep[x]]-1;k>=0;k--){
if(fa[x][k]!=fa[y][k]){
x=fa[x][k],y=fa[y][k];
}
}
return fa[x][0];
}
int main(){
// ios::sync_with_stdio(false);
memset(head,-1,sizeof(head));
cin>>n>>m>>s;
for(int i=1;i<n;i++){
int p,q;
scanf("%d%d",&p,&q);
add(p,q),add(q,p);
}
for(int i=1;i<=n;i++) lg[i]=lg[i-1]+(1<<lg[i-1]==i);//预处理(log_2(i))+1的值
dfs(s,0);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",lca(a,b));
}
return 0;
}