标签:pac std include log bit out == name signed
#include <bits/stdc++.h>
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define PER(I, A, B) for (int I = (A); I >= (B); --I)
#define DB(A) cout<<(A)<<endl;
#define PB push_back
#define Pair pair<int,int>
#define LL long long
using namespace std;
const int maxn=2e5+10;
const int MAX=30;
const int inf=0x3f3f3f3f;
//head
vector<int >a[maxn];
int fa[maxn][MAX];
int dep[maxn];
int lg[maxn];
void dfs(int now,int fat)
{
fa[now][0]=fat;
dep[now]=dep[fat]+1;
FOR(i,1,lg[dep[now]])
{
fa[now][i]=fa[fa[now][i-1]][i-1];
}
for (auto to:a[now]) if (to!=fat)
{
dfs(to,now);
}
}
int lca(int x,int y)
{
if (dep[y]>dep[x]) swap(x,y);
while (dep[x]>dep[y])
{
x=fa[x][lg[dep[x]-dep[y]]];
}
if (x==y) return x;
PER(i,lg[dep[x]],0) if (fa[x][i]!=fa[y][i])
{
x=fa[x][i];
y=fa[y][i];
}
return fa[x][0];
}
signed main()
{
int n;
scanf("%d",&n);
FOR(i,1,n)
{
int x,y;
scanf("%d%d",&x,&y);
a[x].PB(y);
a[y].PB(x);
}
FOR(i,1,n)
{
lg[i]=(int)log2(i);
}
dfs(1,0);
}
标签:pac std include log bit out == name signed
原文地址:https://www.cnblogs.com/reshuffle/p/12322672.html