题目大意
大数据的,并查集模板,路径压缩, 输出几个连通块
#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
int pre[50005];
int ret;
int find_(int x){
int t = x;
while(pre[t] != t){
t = pre[t];
}
int k = x;
int m;
while(k != t){
m = pre[k];
pre[k] = t;
k = m;
}
return k;
}
bool join_(int x, int y){
int xx = find_(x);
int yy = find_(y);
if(xx != yy){
pre[yy] = xx;
ret--;
return false;
}
return true;
}
int main(){
ios::sync_with_stdio(false);
// freopen("D.txt", "r", stdin);
int n ,m;
int cc = 0;
while(scanf("%d%d", &n, &m) && n + m){
cc++;
ret = n;
for(int i = 0; i <= n; i++){
pre[i] = i;
}
int a, b;
for(int i = 0; i < m; i++){
scanf("%d%d", &a, &b);
join_(a, b);
}
printf("Case %d: %d\n", cc, ret);
}
}