HDU 1829 A Bug's Life(基础种类并查集)
来源:互联网 发布:unity3d 2d地图 编辑:程序博客网 时间:2024/05/23 01:15
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829
题意:有n只虫子,和m对性别不同的情侣,判断是否出现性别矛盾
思路:如果你做过poj1182食物链就会发现这题简单的不行!!
和poj1182一样的思路,具体可详见那题的题解:http://blog.csdn.net/qq_36782366/article/details/75014486
主要是处理关系上的方法 运用了类似于关系有向相加的公式
直接上代码
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define maxn 2000struct node{int pre;int relation;}p[1000000];int n,m;int find(int x){int r;if(x==p[x].pre){return x;}r=p[x].pre;p[x].pre=find(r);p[x].relation=(p[x].relation+p[r].relation)%2;//关系的更新return p[x].pre;//返回根节点}int main(){int t;scanf("%d",&t);for(int cas=1;cas<=t;cas++){scanf("%d%d",&n,&m);int a,b;for(int i=0;i<=n;i++){p[i].pre=i;p[i].relation=0;}int flag=0;for(int i=0;i<m;i++){scanf("%d%d",&a,&b);if(flag)continue;int roota=find(a);int rootb=find(b);if(roota!=rootb){//roota->rootb=roota->a+a->b+b->rootbp[rootb].pre=roota;p[rootb].relation=(p[a].relation+1-p[b].relation+2)%2;}else{//a->b=a->roota+roota->rootb+rootb->bif(1!=(2-p[a].relation+p[rootb].relation+p[b].relation)%2)flag=1;}}printf("Scenario #%d:\n",cas);if(flag)printf("Suspicious bugs found!\n");elseprintf("No suspicious bugs found!\n");printf("\n");} return 0;}
阅读全文
0 0
- HDU 1829 A Bug's Life(基础种类并查集)
- HDU 1829 A Bug's Life(基础种类并查集)
- HDU 1829 A Bug's Life(基础种类并查集)
- HDU 1829 A Bug's Life(种类并查集)
- HDU 1829 A Bug's Life【种类并查集】
- HDU 1829 A Bug's Life-基础种类并查集
- hdu 1829 A Bug's Life (种类并查集)
- HDU 1829 A Bug's Life(种类并查集)
- hdu 1829 A Bug's Life(种类并查集)
- HDU 1829 A Bug's Life(种类并查集)
- HDU 1829 A Bug's Life(种类并查集)
- hdu 1829 A Bug's Life (基础并查集)
- HDU1829:A Bug's Life(种类并查集)
- A Bug's Life(种类并查集)
- POJ2492 -A Bug's Life(种类并查集)
- hdu 1829 A Bug's Life(并查集)
- HDU 1829 A Bug's Life (并查集)
- HDU 1829 A Bug's Life (并查集)
- HBase 的那一点知识
- JavaScript事件
- 51nod1240 莫比乌斯函数(普通筛法)
- hdu 1896 优先队列
- 判断文件或者文件夹是否存在
- HDU 1829 A Bug's Life(基础种类并查集)
- struts2中I18N国际化
- Linux GNU Makefile之初识
- JAVA线程知识
- [LeetCode] 461.Hamming Distance 备忘
- SQL中Group By的使用
- ps命令
- TCP协议如何来保证传输的可靠性
- python rsa 实践