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. Visual Studio 2012 编译C++显示cl命令

    为了用newlisp来实现VC编译,以便用我的Emacs开发VC程序,而不需要再打开VS 2012, 需要自己实现命令行的编译.我不需要nmake,因为我想直接了解VC编译器,以便今后更好的驾驭它. ...

  2. (转)iOS Wow体验 - 第八章 - 易用性与自动化技术

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第八章译文精选,也是全书译文的最后一篇.上一篇:W ...

  3. mysql 1449 : The user specified as a definer (&#39;root&#39;@&#39;%&#39;) does not exist 解决方法

    权限问题,授权 给 root  全部sql 权限 mysql> grant all privileges on *.* to root@"%" identified by & ...

  4. [RxJS] Updating Data with Scan

    You often need to update the data flowing through the stream with custom logic based on what you nee ...

  5. [Regular Expressions] Find Repeated Patterns

    Regular Expression Quantifiers allow us to identify a repeating sequence of characters of minimum an ...

  6. 页面显示磁盘空间使用情况-Agedu

    下载:http://www.chiark.greenend.org.uk/~sgtatham/agedu/ [root@localhost ~]# tar zxvf agedu-r9723.tar.g ...

  7. Java中Thread类的start()和run()的区别

    1.start()方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码. 通 过调用Thread类的start()方法来启动一个线程,这时此线程是处于就绪 ...

  8. linux下安装软件的方法

    1. 区分 rpm -qi -qf -ql -qa四个不同选项组合的作用?rpm -qi //查询已经安装的某个RPM软件包的信息rpm -qf //查询某个程序文件是由哪个RPM软件包安装的rpm ...

  9. 限制内容长度(CSS,jQuery)

    CSS(宽度限制在100px之内,超出就会点点点) <style type="text/css"> p{width: 100px;display: inline-blo ...

  10. DevExpress ASPxHtmlEditor控件格式化并导出Word (修复中文字体导出丢失)

    在前台页面中先插入一个ASPxHtmlEditor控件,名为ASPxHtmlEditor1. 我用的Dev版本为14.1 格式化文本 在后台插入如下代码  1     const string css ...