LCA 倍增算法模板

.

 1 #include <cstring>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <vector>
 7 using namespace std;
 8 const int N=10000+5;
 9 vector <int> son[N];
10 int T,n,depth[N],fa[N][20],in[N],a,b;
11 void dfs(int prev,int rt){
12     depth[rt]=depth[prev]+1;
13     fa[rt][0]=prev;
14     for (int i=1;i<20;i++)
15         fa[rt][i]=fa[fa[rt][i-1]][i-1];
16     for (int i=0;i<son[rt].size();i++)
17         dfs(rt,son[rt][i]);
18 }
19 int LCA(int x,int y){
20     if (depth[x]<depth[y])
21         swap(x,y);
22     for (int i=19;i>=0;i--)
23         if (depth[x]-(1<<i)>=depth[y])
24             x=fa[x][i];
25     if (x==y)
26         return x;
27     for (int i=19;i>=0;i--)
28         if (fa[x][i]!=fa[y][i])
29             x=fa[x][i],y=fa[y][i];
30     return fa[x][0];
31 }
32 int main(){
33     scanf("%d",&T);
34     while (T--){
35         scanf("%d",&n);
36         for (int i=1;i<=n;i++)
37             son[i].clear();
38         memset(in,0,sizeof in);
39         for (int i=1;i<n;i++){
40             scanf("%d%d",&a,&b);
41             son[a].push_back(b);
42             in[b]++;
43         }
44         depth[0]=-1;
45         int rt=0;
46         for (int i=1;i<=n&&rt==0;i++)
47             if (in[i]==0)
48                 rt=i;
49         dfs(0,rt);
50         scanf("%d%d",&a,&b);
51         printf("%d\n",LCA(a,b));
52     }
53     return 0;
54 }

猜你喜欢

转载自www.cnblogs.com/pangbi/p/11892863.html