poj2524
2011-05-17 19:23
281 查看
简单并查集
View Code
View Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; #define maxn 50004 int father[maxn]; int getanc(int a) { if (father[a] == a) return a; return father[a] = getanc(father[a]); } void merge(int a, int b) { if (father[a] == -1) father[a] = a; if (father[b] == -1) father[b] = b; father[getanc(a)] = getanc(b); } int main() { // freopen("t.txt", "r", stdin); int t = 0; int n, m; while (scanf("%d%d", &n, &m), n | m) { memset(father, -1, sizeof(father)); t++; for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; merge(a, b); } int ans = 0; for (int i = 0; i < n; i++) if (father[i] == -1 || father[i] == i) ans++; printf("Case %d: %d\n", t, ans); } return 0; }
相关文章推荐
- poj2524
- POJ2524,Ubiquitous Religions
- hdu1232 畅通工程 && poj2524 Ubiquitous Religions(并查集裸)
- POJ2524--Ubiquitous Religions(并查集)
- poj2524
- poj2524
- poj2524 2010.2.8
- poj2524(并查集)
- 并查集入门题集 POJ1611 POJ1703 POJ2524 POJ1182
- POJ2524(简单并查集)
- 并查集-poj2524-Ubiquitous Religions
- POJ2524 并查集
- poj2524
- POJ2524-宗教问题-并查集-ACM
- 第四周周赛——我查,我查,我查查查题解(来自poj2524,1664,1182,HDU1021,5524,5645)
- 【poj2524】Ubiquitous Religions 【Alberta Collegiate Programming Contest 2003.10.18】
- POJ2524_并查集
- POJ2524-宗教问题-并查集-ACM
- POJ2524:Ubiquitous Religions (并查集模板)
- 并查集入门题 hd1232 poj2524 hd1233 hd1213