#include<iostream> #include<cstdio> #include<algorithm> #define db(a) cout << #a << " = " << a << endl #define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl #define db3(a, b, c) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << endl #define MAX 50001 using namespace std; int P[MAX], mejor[MAX]; int find(int i) { return P[i] == -1 ? i : P[i] = find(P[i]); } void join(int i, int j) { if (find(i) != find(j)) P[find(i)] = find(j);} void init(int n) { fill(P, P + n, -1);} int main() { int n, m, a, b, cont = 1; while (scanf("%d %d", &n, &m) != EOF && (n || m)) { init(n + 1); for (int i = 0; i < m; i++) { scanf("%d %d", &a, &b); join(a, b); } int res = 0; for (int i = 1; i < n + 1; i++) res += find(i) == i; printf("Case %d: %d\n",cont++ , res); } return 0; }
jueves, 24 de noviembre de 2011
10583 - Ubiquitous Religions, uva
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario