A Bug's Life(POJ-2492)
来源:互联网 发布:网络诈骗多少钱才算 编辑:程序博客网 时间:2024/06/15 01:30
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!
题意:n个虫子,m对关系,每对关系代表着相恋,看分类对不对,不存在同性恋(无真爱。。。。);
思路:对立的并查集,模板题,种类并查集的关键在于与结点与根结点的距离, 如果距离是奇数那么性别就和跟结点相反,如果是偶数就和跟结点性别相同。
ps:&等于%2,但是比%2效率高;
#include<iostream>#include<cstdio>using namespace std;int a[2005];int b[2005];bool flag=true;int find(int x){ if(x==a[x]) return a[x]; int r=find(a[x]); b[x]=(b[a[x]]+b[x])&1;//b[a[x]]+b[x]如果是偶数的话b[x]=0,如果是奇数的话,b[x]=1; a[x]=r; return a[x];}void link(int x,int y){ int fx=find(x),fy=find(y); if(fx==fy) { if(b[x]==b[y]) { flag=false; } return; } a[fx]=fy; b[fx]=(b[x]+b[y]+1)&1;}int main(){ int t,m,n,x,y; scanf("%d",&t); for(int i=1; i<=t; i++) { flag=true; scanf("%d%d",&n,&m); for(int j=0; j<=n; j++) { a[j]=j; b[j]=0; } for(int j=0; j<m; j++) { scanf("%d%d",&x,&y); if(!flag) continue; link(x,y); } printf("Scenario #%d:\n",i); if(flag) printf("No suspicious bugs found!\n\n"); else printf("Suspicious bugs found!\n\n"); }}
阅读全文
0 0
- A Bug's Life(POJ-2492)
- 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
- C语言 结构体指针需要申请内存但函数定义指针不需要申请
- 数据结构实验之栈七:出栈序列判定
- Java知识点总结
- 数据分页显示
- [Array]Triangle
- A Bug's Life(POJ-2492)
- mysql-python 安装错误 fatal error C1083: Cannot open include file: 'config-win.h': No such file or direc
- hdu 1029 Ignatius and the Princess IV(水题)
- 继承
- select模式的IO的事件通知一定是准确的吗?
- ViewFlipper-----AdapterViewFlipper公告栏
- JavaScript解构赋值(代码说明ES6数组, 对象, 函数的解构赋值)
- a标签href属性值javascript:void(0);与#的区别
- 右键新建项的增加和删除