倍增代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+5;
int head[maxn],cnt,root,fx[21],lg[maxn],f[maxn][21],dep,depth[maxn],n,m,s;
struct T
{
int to,nxt;
}edge[2*maxn];
void add(int from,int to)
{
edge[++cnt].to=to;
edge[cnt].nxt=head[from];
head[from]=cnt;
}
void pre()
{
fx[0]=1;
for(int i=1;i<=20;i++)
fx[i]=fx[i-1]<<1;
for(int i=1;i<=n;i++)
lg[i]=lg[i-1]+(fx[lg[i-1]+1]==i);
}
void dfs(int now,int fa)
{
f[now][0]=fa;
depth[now]=++dep;
for(int i=1;i<=lg[dep];i++)
f[now][i]=f[f[now][i-1]][i-1];
for(int i=head[now];i;i=edge[i].nxt)
if(edge[i].to!=fa) dfs(edge[i].to,now);
dep--;
}
int LCA(int x,int y)
{
if(depth[x]<depth[y])
swap(x,y);
while(depth[x]!=depth[y])
x=f[x][lg[depth[x]-depth[y]]];
if(x==y) return