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

第一次就ac了爽死了!

Posted by chenxuan123456789 at 2012-09-09 09:13:43 on Problem 1611
#include <stdio.h>
#include <string.h>
#define len 30010
int father[len],num[len];
void make_set(int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		father[i]=i;
		num[i]=1;
	}
}
int dfs(int x)
{
	return x==father[x]?x:dfs(father[x]);
}
void union_set(int a,int b)
{
	int x=dfs(a);
	int y=dfs(b);
	if(x!=y)
	{
		father[x]=y;
		num[y]+=num[x];
	}
}
int sum()
{
	int f=father[0];
	while(f!=father[f])
		f=father[f];
	return num[f];
}
int main()
{
	int m,n,t,a,b;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		if(m==0&&n==0)
			break;
		make_set(n);
		while(m--)
		{
			scanf("%d",&t);
			if(t>=1)
			{
				scanf("%d",&a);
				t--;
			}
				while(t--)
				{
					scanf("%d",&b);
					union_set(a,b);
				}
		}
		printf("%d\n",sum());
	}
	return 1;
}

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