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 E09610106 at 2010-08-24 09:49:17 on Problem 1988
#include <stdio.h>
int p[30006];
int s[30006];
int findp(int x)
{
	if(p[x]!=x)
	p[x]=findp(p[x]);
	return p[x];
}
int finds(int x)
{
	if(s[x]!=x)
	s[x]=finds(s[x]);
	return s[x];
}
void markset()
{
	int i;
	for(i=1;i<=30000;i++)
	{
		p[i]=i;
		s[i]=i;
	}
}
int main()
{
	int n;
	int i;
	int tmp,tmp1,tmp2;
	int cnt;
	int son,fath;
 	char op[3];
	while(scanf("%d",&n)!=EOF)
	{
	   markset();
	for(i=1;i<=n;i++)
	{
		scanf("%s",op);
		if(op[0]=='M')
		{
			scanf("%d%d",&son,&fath);
			tmp1=findp(son);
			tmp2=finds(fath);
			p[tmp1]=tmp2;
			s[tmp2]=tmp1;	
		}
		else if(op[0]=='C')
		{
			cnt=0;
			scanf("%d",&tmp);		
			while(p[tmp]!=tmp)
			{
                tmp=p[tmp];
				cnt++;
			}
            printf("%d\n",cnt);
		}
	}
    }
	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