poj A Bug's Life(关系并查集)
来源:互联网 发布:c语言输出星号金字塔 编辑:程序博客网 时间:2024/05/17 03:19
A Bug's Life
Description
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Input
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
Sample Input
23 31 22 31 34 21 23 4
Sample Output
Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!
ps:基础的关系并查集,用rank[i]来表示i与根节点的关系,rank[]=1表示同性,rank[]=0表示异性
然后列举所有的情况,推出rank[]的路径压缩表达式和判断条件表达式即可
代码:
#include<stdio.h>#include<string.h>#define maxn 2000+10int pre[maxn],rank[maxn];int flag;void init(){ for(int i=1; i<=maxn; i++) { pre[i]=i; rank[i]=1;//自己与自己的关系为同性 } flag=0;}int Find(int x){ int temp=pre[x]; if(x==pre[x]) return x; pre[x]=Find(temp); rank[x]=(rank[x]+rank[temp]+1)&1; return pre[x];}void join(int x,int y){ int fx=Find(x),fy=Find(y); if(fx!=fy) { pre[fx]=fy; rank[fx]=(rank[x]+rank[y])&1; } else { if(!((rank[x]+rank[y])&1))//如果rank[x]和rank[y]同时为(1,1)或(0,0)时,说明不符合条件 flag=1; }}int main(){ int t,k=1,n,m; scanf("%d",&t); while(k<=t) { scanf("%d%d",&n,&m); init(); int u,v,i; for(i=1; i<=m; i++) { scanf("%d%d",&u,&v); join(u,v); } printf("Scenario #%d:\n",k++); if(flag) printf("Suspicious bugs found!\n"); else printf("No suspicious bugs found!\n"); puts(""); } return 0;}
1 0
- 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(并查集)
- hdu 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(并查集)
- 10+ powerful debugging tricks with Visual Studio
- 【新建项目&使用viewPager】实现一个Android电子书阅读APP
- 一个完整的ssm项目
- 单调递增最长子序列
- 浅析java中BigDecimal大数值类的使用
- poj A Bug's Life(关系并查集)
- AndroidStudio安装报错
- opencv 颜色识别
- POJ 2823 Sliding Window
- HDU 2073 JAVA
- 高精度幂的计算
- sdutacm-最短路径问题
- JAVA类型转换
- LeetCode 240. Search a 2D Matrix II