poj 1330 Nearest Common Ancestors
#include<iostream>
#include<memory.h>
using namespace std;
int node[10001];
int path1[10001],path2[10001];
void parent(int list[],int s,int& top)
{
top=-1;
do
{
list[++top]=s;
s=node[s];
} while (s);
}
int main()
{
int t,n,i,j,p,c;
cin>>t;
while(t--)
{
cin>>n;
memset(node,0,sizeof(node));
for(i=1;i<n;i++)
{
cin>>p>>c;
node[c]=p;
}
int a,b,size1,size2;
bool con=false;
cin>>a>>b;
parent(path1,a,size1);
parent(path2,b,size2);
for(i=0;i<=size1;i++)
{
for(j=0;j<=size2;j++)
if(path1[i]==path2[j])
{
cout<<path1[i]<<endl;
con=true;break;
}
if(con)break;
}
}
return 0;
}