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

大牛帮忙看看 WA 2492过了 谢谢

Posted by xwc_hziee at 2009-07-20 15:05:25 on Problem 1703
#include<cstdio>
using namespace std;
#define MAX 100100
int set[MAX],rank[MAX],opp[MAX];
int n,m;
int findx(int &x)
{
	int r = x;
	while( r != set[r] )
		r=set[r];
	int i = x,j;
	while(i != r)
	{
		j=set[i];
		set[i]=r;
		i=j;
	}
	return r;
}
void merge(int &x,int &y)
{
	int fx=findx(x);
	int fy=findx(y);
	if(rank[fx]>rank[fy])
		set[fy]=fx;
	else if(rank[fy]>rank[fx])
		set[fx]=fy;
	else 
	{
		set[fx]=fy;
		rank[fy]+=1;
	}
}
void makeset()
{
	for(int i=1;i<=n;i++)
	{
		set[i]=i;
		rank[i]=0;
		opp[i]=-1;
	}
}
int main()
{
	int t,i,x,y;
	char ch;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		makeset();
		for(i=1;i<=m;i++)
		{
			getchar();
			scanf("%c",&ch);
			scanf("%d%d",&x,&y);
			if(ch=='D')
			{
				if(opp[x]!=-1)
				{
					merge(opp[x],y);
				}
				else if(opp[y]!=-1)
				{
					merge(opp[y],x);
				}
				opp[x]=y;
				opp[y]=x;
			}
			else 
			{
				if(findx(x)==findx(y))
					puts("In the same gang.");
				else if(findx(x) == findx(opp[y])||findx(y) == findx(opp[x]))
					puts("In different gangs.");
				else 
					puts("Not sure yet.");
			}
		}
	}
	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