POJ 2492 A Bug's Life(并查集+逻辑关系)
来源:互联网 发布:超级基因优化液第二部 编辑:程序博客网 时间:2024/06/13 13:32
大意:
科学家假定这种虫子只存在异性繁殖,然后给出了m组繁殖的虫子,判断它们之间的关系来求是否存在同性繁殖。
思路:
建立关系数组s来判断其与根节点的的关系,0为同性,1为异性。
判断出现同性繁殖的条件为出现一组虫子在一个集合中并且与根节点的性别关系相同,性别不同的情况为正确情况。
在不同集合中时进行合并操作。
TIP:判断出现同性繁殖也需要继续输入,Scenario之间有空行。
#include <iostream>#include <vector>#include <string.h>#include <stdio.h>using namespace std;int n;int f[60000],rank[60000],s[60000];int find(int x,int &s1){ int t=x; s1=0; while(x!=f[x]) { if(s[x]==1) { s1++; if(s1>1) s1=0; } x=f[x]; } f[t]=x; s[t]=s1;//路径压缩 return x;}bool judge(int x,int y){ int sx,sy; int fx=find(x,sx); int fy=find(y,sy); if(fx==fy) { if(sy==sx) return 0; else return 1; } else { f[fy]=fx; if(sy==0&&sx==0) s[fy]=1; else if(sy==0&&sx==1) s[fy]=0; else if(sy==1&&sx==0) s[fy]=0; else if(sy==1&&sx==1) s[fy]=1; return 1; } return 1;}int main(){ int m,t,x,y; int T,c=1; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); int ans=1; for(int i=1; i<=n; i++) f[i]=i,s[i]=0; for(int i=0; i<m; i++) { scanf("%d%d",&x,&y); if(!judge(x,y)) ans=0; } printf("Scenario #%d:\n",c++); if(ans) { printf("No suspicious bugs found!\n"); } else { printf("Suspicious bugs found!\n"); } //if(T!=0) printf("\n"); } return 0;}
0 0
- POJ 2492 A Bug's Life(并查集+逻辑关系)
- poj 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
- 学习计算机方法二
- 8个值得关注的SQL-on-Hadoop框架
- 多线程断点续传实践
- 仿支付宝/微信的密码输入框效果GridPasswordView解析
- POJ 2528 Mayor's posters 线段树+离散化
- POJ 2492 A Bug's Life(并查集+逻辑关系)
- FZU1150 Farmer Bill's Problem
- 黑马程序员——正则表达式2:功能演示
- 基于AbstractRoutingDataSource的动态切换数据库
- FZU1171 Hard to Believe, but True!
- scala柯里化函数
- Jenkins API curl创建job、执行构建
- Find The Multiple
- jquery代码阅读jQuery.makeArray()