/*并查集*/
#include <iostream>
using namespace std;
const int MAXN = 50005;
int father[MAXN];
int n, m, cas = 0, sum;
void init()
{
for (int i = 0; i < n; ++i) {
father[i] = i;
}
return;
}
int getfather(int x)
{
if (x != father[x])
father[x] = getfather(father[x]);
return father[x];
}
void merge(int x, int y)
{
int fx = getfather(x);
int fy = getfather(y);
if (fx != fy) {
father[fy] = fx;
--sum;
}
}
int main()
{
while (cin >> n >> m && (n + m)) {
init();
sum = n;
int x, y;
for (int i = 0; i < m; ++i) {
cin >> x >> y;
merge(x, y);
}
cout << "Case " << ++cas << ": " << sum << endl;
}
return 0;
}
POJ2524 Ubiquitous Religions
最新推荐文章于 2014-09-18 22:26:49 发布