Sample Output
Case 1: 1 Case 2: 7
#include <iostream>
#include"cstdio"
#include"cstring"
#include"cstdlib"
using namespace std;
int bin[50050];
int m,n;
int findx(int x)
{
if(bin[x]!=x)
bin[x]=findx(bin[x]);
return bin[x];
}
void Merge (int x,int y)
{
int fx=findx(x);
int fy=findx(y);
if(fx!=fy)
bin[fx]=fy;
}
void Qsort(int s[],int low,int high)
{
int i=low,j=high;
int t=s[low];
while(low<high)
{
while(low<high&&s[high]<=t)
high--;
if(low<high)s[low]=s[high];
while(low<high&&s[low]>=t)low++;
if(low<high)s[high]=s[low];
}
s[low]=t;
if(low>i)Qsort(s,i,low-1);
if(low<j)Qsort(s,low+1,j);
}
int main()
{int k=0;
while(~scanf("%d%d",&m,&n))
{if(n==0&&m==0)break;k++;
memset(bin,'\0',sizeof(bin));
for(int i=0;i<m;i++)
bin[i]=i;
for(int i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
Merge(x,y);
}
for(int i=0;i<m;i++)
bin[i]=findx(bin[i]);
int cou=1;
Qsort(bin,0,m-1);
for(int i=0;i<m-1;i++)
if(bin[i]!=bin[i+1])
cou++;
printf("Case %d: %d\n",k,cou);
}
return 0;
}