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. c指针点滴-指针与类型

    #include <stdio.h> #include <stdlib.h> //数据通信 void main() { ; int *p1 = &num; int *p ...

  2. User Defined Runtime Attributes

    设置View borderWidth/cornerRadius/borderColor 为了兼容CALayer 的KVC ,你得给CALayer增加一个分类 CALayer+BorderColor.h ...

  3. Web Service工作原理

    Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...

  4. Js获取元素样式值(getComputedStyle&currentStyle)兼容性解决方案

    因为:style(document.getElementById(id).style.XXX)只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的. 一般js获取内部样式和外部样式使用 ...

  5. C# IoC 容器

    Unity是Unity是微软patterns& practices组用C#实现的轻量级,可扩展的依赖注入容器,它为方便开发者建立松散耦合的应用程序, 有以下优点: 1.简化了对象的创建,特别是 ...

  6. Play Framework Web开发教程(33): 结构化页面-组合使用模板

    和你编写代码相似,你编写的页面也能够由多个小的片段组合而成,这些小的片段本身也能够由更小的片段构成.这些小片段一般是能够在其他页面反复使用的:有些部分能够用在全部页面,而有些部分是某些页面特定的.本篇 ...

  7. Dynamics CRM 开发模板使用手册(插件开发)

    CRM开发手册 本手册介绍在Visual Studio 2015 + Dynamics CRM Developer Extensions模板开发环境下,插件和JS脚本的开发.部署与调试过程. 手册中提 ...

  8. JS高级程序设计学习笔记之RegExp类型

    创建正则表达式: 字面量形式定义正则表达式: Var expression = / pattern /flags ;pattern部分可以使任意简单或复杂的正则表达式.每个正则表达式可以带有一个或多个 ...

  9. JS--显示类型转换Number—隐式类型转换

    显示类型转换 (强制类型转换):Number()parseInt()parseFloat() Number是整体转换--能够把一个看起来像数字的字符串转成数字--尽量去转换能转的都转了 var a = ...

  10. (转)HTTP 无法注册 URL http://+:9999/CalculatorService/。进程不具有此命名空间的访问权限

    写WCF时在 1 host.Open(); 报错:HTTP 无法注册 URL http://+:9999/CalculatorService/.进程不具有此命名空间的访问权限(有关详细信息,请参见 h ...