|
#include <iostream> using namespace std;
const int maxn=50001; int father[maxn]; int rank[maxn]; void prepare(int n) { for(int i=1;i<=n;i++) { father[i]=i; rank[i]=i; } } int find(int x) { if(father[x]!=x) father[x]=find(father[x]); return father[x]; } void union_set(int x,int y) { int fx,fy; fx=find(x); fy=find(y); if(rank[fx]>rank[fy]) father[fy]=fx; else if(rank[fx]<rank[fy]) father[fx]=fy; else { father[fy]=fx; rank[fx]++; } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin) ; #endif int n,m,i; int casen=0; while(scanf("%d%d",&n,&m)!=EOF) { casen++; if(n==0&&m==0) break; prepare(n); int sum=0; for(i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); union_set(x,y); } for(i=1;i<=n;i++) if(father[i]==i) sum++; printf("Case %d: %d\n",casen,sum); } return 0; }
|