| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
并查集是好东西!冒失可以用广搜来做!#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator