[kuangbin带你飞] 专题1-23 https://vjudge.net/article/187
专题五 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236
POJ 1611 The Suspects http://poj.org/problem?id=1611
HDU 1213 How Many Tables http://acm.hdu.edu.cn/showproblem.php?pid=1213
HDU 3038 How Many Answers Are Wrong http://acm.hdu.edu.cn/showproblem.php?pid=3038
POJ 1182 食物链 https://vjudge.net/problem/POJ-1182
POJ 1417 True Liars https://vjudge.net/problem/POJ-1417
POJ 1456 Supermarket http://poj.org/problem?id=1456
POJ 1733 Parity game http://poj.org/problem?id=1733
POJ 1984 Navigation Nightmare https://vjudge.net/problem/POJ-1984
POJ 2492 A Bug‘s Life https://vjudge.net/problem/POJ-2492
POJ 2912 Rochambeau https://vjudge.net/problem/POJ-2912
ZOJ 3261 Connections in Galaxy War https://vjudge.net/problem/ZOJ-3261
HDU 1272 小希的迷宫 http://acm.hdu.edu.cn/showproblem.php?pid=1272
POJ 1308 Is It A Tree? http://poj.org/problem?id=1308
// hdu 1272 & poj 1308
// "求树是否是个环" ,水。
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 const int MAXN = 1e3+5; 6 7 inline int read() { 8 char c = getchar(); int s = 0, f = 1; 9 while(c < ‘0‘ || c > ‘9‘) { if(c == ‘-‘) f = -1; c = getchar(); } 10 while(c >= ‘0‘ && c <= ‘9‘) s = s*10 + c-‘0‘, c = getchar(); 11 return s*f; 12 } 13 14 int p[MAXN]; 15 16 inline int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } 17 18 int main() { 19 int s, e; 20 int T = 1; 21 while(1) { 22 bool flag = true; 23 while(1) { //scanf("%d%d", &s, &e) == 2 && s 24 s = read(), e = read(); 25 if(s == 0 && e == 0) break; 26 if(s == -1) return 0; 27 if(!p[s]) p[s] = s; 28 if(!p[e]) p[e] = e; 29 int x = find(s); 30 int y = find(e); 31 if(x == y) flag = false; 32 else p[x] = y; 33 } 34 int ans = 0; 35 for(int i = 1; i <= MAXN; ++i) { 36 if(p[i] == i) ans++; 37 p[i] = 0; 38 } 39 40 printf("Case %d is ", T++); 41 if(!flag || ans > 1) printf("not a tree.\n"); 42 else printf("a tree.\n"); 43 } 44 return 0; 45 }
跟着chengyulala刷题之[kuangbin带你飞]之'并查集'专题/斜眼笑
原文地址:https://www.cnblogs.com/pupil-xj/p/11614643.html