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 chenxuan123456789 at 2012-08-06 20:25:52 on Problem 2524
#include <stdio.h>
#include <string.h>
int father[50010];
int n,result;
void make_set()
{
	int i;
	for(i=1;i<=n;i++)
	father[i]=i;
}
int find_set(int x)
{
  if(father[x]==x)
	  return x;
  else
	  return find_set(father[x]);
}
void union_set(int a,int b)
{
	a=find_set(a);
	b=find_set(b);
	if(a==b)
	return;
	result--;
	if(a!=b)
	father[b]=a;
}
int main()
{
	int m,i,a,b,times=1;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		if(n==0&&m==0)
		break;
		make_set();
	    result=n;
		while(m--)
		{
			scanf("%d %d",&a,&b);
			union_set(a,b);
		}
		printf("Case %d: %d\n",times++,result);
	}
	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