#include <stdio.h>
int stack[500000];
int find(int x)
{
int r,j,k;
r=x;
while(stack[r]!=r)
r=stack[r];
k=x;
while(k!=r)
{
j=stack[k];
stack[k]=r;
k=j;
}
return r;
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
stack[fx]=fy;
}
int main()
{
int n,m,i,x,y;
int count =0;
while(scanf("%d %d",&n,&m)&&n!=0&&m!=0)
{
count++;
for(i=1; i<=n; i++)
stack[i]=i;
for(i=1; i<=m; i++)
{
scanf("%d %d",&x,&y);
merge(x,y);
}
int c=0;
for(i=1; i<=n; i++)
if(stack[i]==i)
c++;
printf("Case %d: %d\n",count,c);
}
}
Ubiquitous Religions
最新推荐文章于 2020-02-13 09:54:54 发布