Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
救命啊!我真的不知道哪里错了,希望各位大虾看看。 想法很简单,就是看x是否是祖先,不是则将其父结点赋给它,y也一样。 #include<stdio.h> int main() { int T,n,i,j,k,x,y,a[11000]; scanf("%d",&T); for(i=0;i<T;i++) { scanf("%d",&n); for(j=1;j<=n;j++) a[j]=0; for(j=0;j<n-1;j++) { scanf("%d%d",&x,&y); a[y]=x; } scanf("%d%d",&x,&y); while(1) { if(x==y) { printf("%d\n",x); break; } k=y; while(1) { if(a[k]==0) break; if(a[k]==x) break; k=a[k]; } if(a[k]==x) { printf("%d\n",x); break; } if(a[x]>0) x=a[x]; k=x; while(1) { if(a[k]==0) break; if(a[k]==y) break; k=a[k]; } if(a[k]==y) { printf("%d\n",y); break; } if(a[y]>0) y=a[y]; } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator