POJ 2495 A Bug's Life【并查集扩展】
来源:互联网 发布:原始股 购买 知乎 编辑:程序博客网 时间:2024/05/22 12:22
题意: 找 gay 。
给你n个人,再给你m次“交易”关系。
让你找出有没有人”交易“不正常;
题解:
并查集,两人共同祖先的话,比较关系,
否则合并。
要另外开一个数组。记录此点的性别(与祖先有关系,其实就是多少辈的关系)在find()中每次更新,
#include<iostream>#include<string>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn=2020;int n,m,T,a,b;int fa[maxn],rela[maxn];int Find(int x){ if(fa[x]==x) return x; int t=fa[x]; fa[x]=Find(fa[x]); rela[x]=(rela[x]+rela[t])%2; return fa[x];}int main(){ scanf("%d",&T); for(int ca=1;ca<=T;++ca){ scanf("%d %d",&n,&m); for(int i=1;i<=n;++i) fa[i]=i,rela[i]=0; int flag=0; while(m--){ scanf("%d %d",&a,&b); if(flag) continue; int x=Find(a),y=Find(b); if(x==y){ if(rela[a]==rela[b]) flag=1; }else { fa[x]=y; rela[x]=(rela[a]+rela[b]+1)%2; } } printf("Scenario #%d:\n",ca); if(flag)printf("Suspicious bugs found!\n"); else printf("No suspicious bugs found!\n"); if(m) printf("\n"); } return 0;}
0 0
- POJ 2495 A Bug's Life【并查集扩展】
- [POJ 2492] A Bug's Life [扩展并查集]
- POJ2492 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 P2492 A Bug's Life 并查集
- 【种类并查集】-POJ-2492-A bug's life
- POJ 2492 A Bug's Life(并查集)
- POJ-2492 A Bug's Life 并查集
- 51nod1711 平均数
- jetty web javax.el.ExpressionFactory.newInstance()Ljavax/el/ExpressionFactory
- Hibernate之底层原理的7点整理和总结
- oracle 提取中文字符串拼音首字母函数,拼音简码提取函数
- SpringMVC-15 Spring整合SpringMVC
- POJ 2495 A Bug's Life【并查集扩展】
- 《统计学习方法》第五章决策树学习笔记
- 实现匹配文件(并自定义命名)的批量拷贝,后续上传改良版
- 关于IE11不能new date,转换时间的方法
- Spring 4 常见异常总结
- 关于双硬盘电脑装Windows纯净系统的问题(已解决)
- 关于ScrollView嵌套listView默认位置为listview顶部的问题
- 【数据结构笔记】1:树和森林,并查集中的部分函数
- centos 行 2: $'\r': 未找到命令 在window 上编写的 sh