#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn = 10010;
int t,n;
int x,y;
int father[maxn],bj[maxn];
void bl(int n)
{
while(n !=father[n])
{
bj[n] = 1;
n = father[n];
}
bj[n] = 1;
return;
}
int sh(int m)
{
while(bj[m] != 1)
{
m = father[m];
}
return m;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
father[i] = i;
}
for(int i=1;i<=n-1;i++)
{
int a,b;
scanf("%d%d",&a,&b);
father[b] = a;
}
memset(bj,0,sizeof(bj));
scanf("%d%d",&x,&y);
bl(x);
int ans = sh(y);
printf("%d\n",ans);
}
return 0;
}
并查集 POJ 1330
最新推荐文章于 2018-09-17 21:58:58 发布