标签:
Time Limit: 1000ms Memory Limit: 32Mb
Description
Input
Output
Sample Input
2 16 1 14 8 5 10 16 5 9 4 6 8 4 4 10 1 13 6 15 10 11 6 7 10 2 16 3 8 1 16 12 16 7 5 2 3 3 4 3 1 1 5 3 5
Sample Output
4 3
Source
1 #include<stdio.h> 2 #include<string.h> 3 using namespace std; 4 int fa[10001]; 5 bool vx[10001]; 6 int main() { 7 int T; 8 scanf("%d",&T); 9 while(T--) { 10 int n;scanf("%d",&n); 11 memset(fa,-1,sizeof(fa)); 12 memset(vx,0,sizeof(vx)); 13 for (int i=1;i<=n-1;++i) { 14 int a,b;scanf("%d%d",&a,&b); 15 fa[b]=a; 16 } 17 int u,v; 18 scanf("%d%d",&u,&v); 19 while(u!=-1) { 20 vx[u]=1; 21 u=fa[u]; 22 } 23 while(!vx[v]) v=fa[v]; 24 printf("%d\n",v); 25 } 26 return 0; 27 }
[POJ 1330] Nearest Common Ancestors (朴素方法)
标签:
原文地址:http://www.cnblogs.com/TonyNeal/p/poj1330_1.html