Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

救命啊!

Posted by shangmin at 2005-10-10 22:15:03 on Problem 1330
我真的不知道哪里错了,希望各位大虾看看。
想法很简单,就是看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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator