裸LCA...n*log(2,n)的预处理...每次查询log(2,n)..方法来自 Here
Program:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define oo 2000000000
#define pi acos(-1)
using namespace std;
int n,p[10005][18],t[10005],h;
bool son[10005];
void gett(int x)
{
if (p[x][0]==x)
{
h=1;
t[x]=h;
return;
}
gett(p[x][0]);
t[x]=++h;
return;
}
int LCA(int x,int y)
{
int k;
if (t[y]>t[x])
{
k=y; y=x; x=k;
}
while (t[x]>t[y])
{
k=0;
while (t[p[x][k]]>t[y]) k++;
if (t[p[x][k]]==t[y])
{
x=p[x][k];
break;
}
x=p[x][k-1];
}
while (x!=y)
{
k=0;
while (p[x][k]!=p[y][k]) k++;
if (k) k--;
x=p[x][k];
y=p[y][k];
}
return x;
}
int main()
{
int T,i,x,y;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
for (i=1;i<=n;i++) p[i][0]=i;
memset(son,false,sizeof(son));
for (i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
p[y][0]=x;
son[x]=true;
}
for (i=1;i<=n;i++)
if (!son[i]) gett(i);
for (y=1;y<=16;y++)
for (i=1;i<=n;i++)
p[i][y]=p[p[i][y-1]][y-1];
scanf("%d%d",&x,&y);
printf("%d\n",LCA(x,y));
}
return 0;
}