题目大意
和上一篇HDU1213一样,最基本的并查集题目
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
const int M = 50005;
int id[M];
int count;
int find(int p)
{
while(p != id[p]) p = id[p];
return p;
}
bool connected(int p,int q)
{
return find(p) == find(q);
}
void unionn(int p,int q)
{
int pRoot = find(p);
int qRoot = find(q);
if(pRoot == qRoot) return;
id[pRoot] = qRoot;
--count;
}
int main()
{
int n,m;
int ans = 0;
while(scanf("%d%d",&n,&m))
{
if(n == 0 && m == 0)
break;
count = n;
for(int i = 1;i <= n; i++)
id[i] = i;
while(m--)
{
int p,q;
scanf("%d%d",&p,&q);
unionn(p,q);
}
printf("Case %d: %d\n",++ans,count);
}
return 0;
}