http://uva.onlinejudge.org/external/105/10583.html
很簡單的Disjoint Set
最後輸出Case n: ans
C是大寫= = 害我WA了兩次
另外Uva真的是ANSI C..
註解只能用
/* This is a comment. */
不能 用
// Not allowed in ANSI C
害我吃了一次CE…
int group;
int set[50001];
int rank[50001];
void set_init(int n)
{
int i;
for (i = 1; i <= n; i++) {
set[i] = i;
rank[i] = 1;
}
return ;
}
int find(int x)
{
if (set[x] == x) {
return x;
} else {
return set[x] = find(set[x]);
}
}
void unite(int a, int b)
{
a = find(a);
b = find(b);
if (a == b) {
return ;
}
group--;
if (rank[a] < rank[b]) {
set[a] = b;
} else {
set[b] = a;
if (rank[a] == rank[b]) {
rank[a]++;
}
}
}
int main()
{
int n, m, i, j, count = 0;
while (scanf("%d%d", &n, &m) != EOF && n && m) {
count++;
group = n;
set_init(n);
while (m--) {
scanf("%d%d", &i, &j);
unite(i, j);
}
printf("Case %d: %d\n", count, group);
}
return 0;
}