A Bug's Life                 
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. 
 

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

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
 

Sample Output

Scenario #1:
Suspicious bugs found!
 
Scenario #2:
No suspicious bugs found!  
 
题意:有N个小虫,编号1->N,给出M对交配的小虫编号,按理来说,应该是异性交配,但题目要求的就是是否会出现同性恋的情况,比如A和B交配,A,B是不同性别,B和C交配,B、C性别不同,如果A能和C交配,显然A,C是同性。
解析:并查集,将编号扩大两倍,1->N代表每个小虫,N+i代表i对应的另一性别的自己。对于每对编号a,b;先检查root(a)==root(b),如果相等,说明是同性恋,否则将root(a)与root(b+N)合并,root(b)与root(a+N)合并,相当于把同性的并在一起。
代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=;
const double eps=0.00000001;
int d[];
int root(int a)
{
while(d[a]!=a) a=d[a];
return a;
}
int main()
{
int T,kase=;
cin>>T;
while(T--)
{
int N,M;
cin>>N>>M;
for(int i=;i<=*N;i++) d[i]=i;
bool ok=true;
for(int i=;i<=M;i++)
{
int from,to;
scanf("%d%d",&from,&to);
if(!ok) continue;
int ra=root(from);
int rb=root(to);
if(ra==rb) ok=false; //判断是否为同性恋
ra=root(from);
rb=root(to+N);
d[ra]=rb; //合并
ra=root(from+N);
rb=root(to);
d[ra]=rb;
}
printf("Scenario #%d:\n",++kase); if(!ok) cout<<"Suspicious bugs found!"<<endl;
else cout<<"No suspicious bugs found!"<<endl;
printf("\n");
}
return ;
}
 
 

hdu 1829 A Bug's Life(并查集)的更多相关文章

  1. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  2. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

  3. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  4. [HDU 3712] Fiolki (带边权并查集+启发式合并)

    [HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...

  5. HDU 1829 A Bug's Life (种类并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Oth ...

  6. hdu 1829 A Bug's Life(分组并查集(偏移量))

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  8. HDU 1829 - A Bug's Life

    Problem Description Background Professor Hopper is researching the sexual behavior of a rare species ...

  9. hdu 5652 India and China Origins 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...

随机推荐

  1. VC6.0建立控制台程序实现PDA应用

    作者:iamlaosong 由于须要,又写起了文本界面的程序,以便PDA通过telnet连上运行. 假设是Linuxserver的话.这是非常easy的事,但是用户server是windows ser ...

  2. OLE操作Excel编译错误处理

      Excel在公司用的很多,而这个东西我用的不是很好,就想用程序来处理,遇到很多错误.这几天研究了下OLE操作Excel.环境:VS2008 SP1+Excel 2007 加入OLE Type Li ...

  3. 多路复用I/O epoll()

    epoll 是Linux内核中的一种可扩展IO事件处理机制,最早在 Linux 2.5.44内核中引入,可被用于代替POSIX select 和 poll 系统调用,并且在具有大量应用程序请求时能够获 ...

  4. static \ const \ volatile 的含义

    1.static 在函数体内,一个被声明为静态的变量在这一函数被调用的过程中维持其值不变 在模块内(函数体外),一个被声明为静态的变量可以被模块内的所有函数访问,但不能被模块外的其他函数访问,即它是一 ...

  5. Private Members in JavaScript

    Private Members in JavaScript Douglas Crockford www.crockford.com JavaScript is the world's most mis ...

  6. Struts2 技术全总结 (正在更新)

    背景:Struts1是一个高度成熟的框架,运行效率高,但其致命缺陷在于与JSP/Servlet的耦合非常紧密,因而导致了一些严重问题.其次,Struts1与Servlet API的严重耦合,使应用难以 ...

  7. OpenGL ES 2.0 雾

    在场景中使用雾不但可以提高真实感,特定的情况下还能优化性能.具体是指当物体离摄像机足够远时,雾就足够浓,此时只能看到雾而看不到物体,也就不必对物体着色进行详细计算,这样可以大大提高渲染效率. 雾有很多 ...

  8. Emacs显示行号

    在配置.emacs文件中加上    (global-linum-mode t) 启动emacs后按     m-x  global-linum-mode 就可以显示行号,但是每次打开emacs,要重新 ...

  9. CentOS FTP服务器权限控制

    在默认配置下,本地用户登入FTP后可以使用cd命令切换到其他目录,这样会对系统带来安全隐患.可以通过以下三条配置文件来控制用户切换目录. chroot_list_enable=YES/NO(NO) 设 ...

  10. ubuntu中安装jdk

    1.下载jdk压缩包: http://download.oracle.com/otn-pub/java/jdk/7u55-b13/jdk-7u55-linux-x64.tar.gz 2.解压缩jdk ...