标签:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 26629 | Accepted: 13096 |
10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0
Case 1: 1
Case 2: 7
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define REP(i, s, n) for(int i = s; i <= n; i ++) #define REP_(i, s, n) for(int i = n; i >= s; i --) #define MAX_N 50000 + 10 using namespace std; int n, m; int F[MAX_N]; int find(int x){ if(x == F[x]) return x; return F[x] = find(F[x]); } void Union(int x, int y){ x = find(x), y = find(y); if(x == y) return; F[y] = x; } int main(){ int x, y, T = 0; while(scanf("%d%d", &n, &m) != EOF){ if(n == 0 && m == 0) break; int res = 0; REP(i, 1, n) F[i] = i; while(m --){ scanf("%d%d", &x, &y); Union(x, y); } REP(i, 1, n) F[i] = find(i); sort(F + 1, F + n + 1); REP(i, 1, n - 1) if(F[i] != F[i + 1]) res ++; printf("Case %d: %d\n", ++ T, res + 1); } return 0; }
标签:
原文地址:http://www.cnblogs.com/ALXPCUN/p/4552994.html