[NWPU][2014][TRN][12]并查集D - A Bug's Life POJ 2492
来源:互联网 发布:seo h1标签 编辑:程序博客网 时间:2024/06/05 09:58
#include <iostream>#include<cstdio>using namespace std;const int MAX_N = 2005;int par[MAX_N];//父亲int rank[MAX_N];//树的高度//初始化n个元素void init(int n){ for(int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; }}//查询树的根int find(int x){ if(par[x] == x) return x; else return par[x] = find(par[x]);}//合并x和y所属的集合void unite(int x, int y){ x = find(x); y = find(y); if(x == y) return ; if(rank[x] < rank[y] ) par[x] = y; else { par[y] = x; if(rank[x] == rank[y]) rank[x]++; }}//判断x和y是否属于同一个集合bool same(int x, int y){ return find(x) == find(y);}int main(){ //freopen("input.txt", "r", stdin); int cas, n, k, ans = 0; //cin >> cas; scanf("%d", &cas); for(int i = 1; i <= cas; i++) { ans = 0; //cin >> n >> k; scanf("%d%d", &n, &k); init(n * 4); while(k--) { int a, b; //cin >> a >> b; scanf("%d%d", &a, &b); a -= 1; b -= 1; if(same(a, b)) { ans++; } else { unite(a, b + n); unite(a + n, b); //此处为啥要这样写?不懂 } } if(ans == 0) //cout << "Scenario #" << i << ":\n" << "No suspicious bugs found!" << endl; //else cout << "Scenario #" << i << ":\n" << "Suspicious bugs found!" << endl; printf("Scenario #%d:\nNo suspicious bugs found!\n\n", i); else printf("Scenario #%d:\nSuspicious bugs found!\n\n", i); } return 0;}
0 0
- [NWPU][2014][TRN][12]并查集D - A Bug's Life POJ 2492
- poj 2492 A Bug's Life(并查集)
- POJ 2492 A Bug's Life(并查集)
- poj 2492 并查集 A Bug's Life
- POJ-2492 a bug's life 并查集
- poj 2492 并查集 a bug's life
- POJ 2492 A Bug's Life (并查集)
- poj 2492 A Bug's Life(种类并查集)
- poj 2492 -- A Bug's Life(并查集)
- POJ 2492 A Bug's Life (并查集)
- poj 2492 - A Bug's Life(并查集)
- 【种类并查集】-POJ-2492-A bug's life
- POJ 2492 A Bug's Life(并查集)
- POJ-2492 A Bug's Life 并查集
- [POJ 2492] A Bug's Life [扩展并查集]
- poj 2492 A Bug's Life 【并查集拓展】
- poj 并查集 - 2492 A Bug's Life
- POJ 2492 A Bug's Life 并查集
- “Spark上流式机器学习算法实现”终期检查报告
- 2013 ACM/ICPC Asia Regional Chengdu Online
- DLUTOJ 1184 分块
- HTTP API设计Guide
- java经典编程题(43-45)
- [NWPU][2014][TRN][12]并查集D - A Bug's Life POJ 2492
- 题目1518:反转链表
- NOPI使用手册
- android 设置全屏
- 如何使用hadoop实现精准营销
- 拓扑排序
- error C2600: 'Point::Point' : cannot define a compiler-generated special member function (must be de
- 循环链表-约瑟夫环(程序员面试宝典修正代码)
- Git详解