poj 2492 A Bug's Life(并查集)
来源:互联网 发布:天敏智能网络机顶盒价格 编辑:程序博客网 时间:2024/04/28 05:58
题意:
有两种性别的bug,研究发现他们每个bug只和不同性别的人交流,给出一些bug之间交流记录,问这个研究是否正确
解题思路:
定义ran[i]记录i与祖先的关系,用带权并查集来存储bug之间的关系。如果交流记录里出现有两个bug在同一棵树上了,而且他们还是同一个性别,那么说明研究不正确,如果没有这样的两个bug,那么说明研究正确。
代码:
#include <iostream>#include <cstdio>using namespace std;const int maxn=1e5+5;int f[maxn];int ran[maxn];int n;void init(){ int i; for(i=1; i<=n; i++)f[i]=i,ran[i]=0;}int Find(int x){ if(x==f[x])return x; else { int y=Find(f[x]); ran[x]=(ran[x]+ran[f[x]])%2; return f[x]=y; }}int merg(int x, int y){ int xx=Find(x); int yy=Find(y); if(xx!=yy) {f[xx]=yy;ran[xx]=(2-ran[x]+1+ran[y])%2; }}int main(){ int t, e=1; cin>>t; while(t--) { int m; scanf("%d%d", &n, &m); int i; init(); int x, y; int ans=0; for(i=0; i<m; i++) {scanf("%d%d", &x, &y);if(ans)continue;int xx, yy;xx=Find(x);yy=Find(y);if(xx==yy){if((ran[x]-ran[y]+2)%2==0){ ans=1;}}else merg(x, y); } printf("Scenario #%d:\n", e++); if(ans)printf("Suspicious bugs found!\n"); else printf("No suspicious bugs found!\n"); if(t>0)printf("\n"); }}
0 0
- 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 [扩展并查集]
- poj 2492 A Bug's Life 【并查集拓展】
- iOS中ARC与MRC代码的兼容
- 01背包 高数Umaru系列(9)——哈士奇
- pc与移动端互相跳转
- 前端性能优化23条
- iOS 10下网络请求返回Error code=-1005问题修复
- poj 2492 A Bug's Life(并查集)
- [Linux命令]--mkdir
- 腾讯超全干货!如何快速有效的发散思维?
- Android 如何让EditText不自动获取焦点
- Kafka Consumer接口
- Java基础知识学习记录
- Andriod 应用两种设计风格
- SQL select distinct语句
- 代理设计模式之静态代理与动态代理