A Bug's Life

Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 11386 Accepted Submission(s): 3709

Problem 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.
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!
Hint
Huge input,scanf is recommended.
Source
Recommend
linle | We have carefully selected several similar problems for you:

pid=1558" target="_blank">
1558

pid=1811" target="_blank">
1811

pid=1325" target="_blank">
1325
2473 3172



#include<stdio.h>
int root[2010];
int rank[2010];
int find_set(int x){
if(root[x]==x) return x;
int t=root[x];
root[x]=find_set(t);
rank[x]=(rank[x]+rank[t])%2;
return root[x];
}
void unio(int a,int b){
int t,k;
t=root[a];
k=root[b];
root[t]=k;
rank[t]=(rank[b]+1-rank[a]);
return ;
}
int main(){
int t,cas=0;;
scanf("%d",&t);
while(t--){
printf("Scenario #%d:\n",++cas);
int n,m,i,ok=0,a,b;
scanf("%d%d",&n,&m);
for(i=0;i<=n;++i){
root[i]=i;
rank[i]=0;
}
for(i=0;i<m;++i){
scanf("%d%d",&a,&b);
if(!ok){
int pa,pb;
pa=find_set(a) ;
pb=find_set(b);
if(pa==pb&&(rank[a]==rank[b])){
ok=1;
}
else
unio(a,b);
}
}
if(ok)
printf("Suspicious bugs found!\n\n");
else
printf("No suspicious bugs found!\n\n"); }
return 0;
}


hdoj-1289-A Bug&#39;s Life【种类并查集】的更多相关文章

  1. A Bug's Life(种类并查集)(也是可以用dfs做)

    http://acm.hdu.edu.cn/showproblem.php?pid=1829   A Bug's Life Time Limit:5000MS     Memory Limit:327 ...

  2. 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 ...

  3. POJ2492:A Bug's Life(种类并查集)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 45757   Accepted: 14757 题 ...

  4. A Bug's Life(hdu1829种类并查集)

    题意:有一群虫子,现在给你一些关系,判断这些关心有没有错 思路:向量种类并查集,下面讲一下向量的种类并查集 本题的各个集合的关心有两种0同性,1异性,怎么判断有错, 1.先判断他们是否在一个集合,即父 ...

  5. hdu 1182 A Bug's Life(简单种类并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...

  6. hdu1829&&poj2492 A Bug's Life 基础种类并查集

    把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续 #include <cstdio> #include <cstring> ...

  7. hdu1829 A Bug's Life 基础种类并查集

    题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合(等价类),异性的异性为同性. #include<iostream> #i ...

  8. POJ-2492 A Bug's Life(种类并查集)

    http://poj.org/problem?id=2492 题意: 给出一个T代表几组数据,给出一个n一个m,代表人的编号由1~n,m条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令 ...

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

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

  10. 【POJ】2492 A bug's life ——种类并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 De ...

随机推荐

  1. Unity经验之谈

    1.全屏与非全屏之间的切换 if (Input.GetMouseButtonDown(1)) { Screen.fullScreen = !Screen.fullScreen; } 2.Camera适 ...

  2. C语言操作数截断

    //测试截断 #include <stdio.h> int main() { int a = 0x80000001; unsigned int b = 0x80000001; printf ...

  3. UnrealEngine4编码风格的思考

    第一次拿到UE4源码,扫了一遍.各种宏定义,各种模板,各种类层次.杂乱无章. 后来慢慢明确其规律: UE4的编码风格是在匈牙利命名法的基础下做了改进,使其更适用游戏引擎业务(业务特点:数据可视编辑.脚 ...

  4. Tensorflow MNIST 数据集測试代码入门

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50614444 測试代码已上传至GitH ...

  5. STL使用————SET&MULTISET

    SET函数的基本用法 by hhl 使用set的好处 1. 当增加元素后,集合会自动删重并从小到大排列(时间比快排还快)2. 相当于一棵伸展树(能快速求出后继) 使用基础 #include<se ...

  6. POJ 3184 DP+剪枝

    思路: 先找到每i头奶牛能在的位置 (一段区间) 记为L[i]和R[i] f[j]表示在位置j取到的最小值 每回在范围内更新一哈 //By SiriusRen #include <cstdio& ...

  7. Gym - 100203I I WIN 网络流

    Gym - 100203I  I WIN 题意:一个n*m的矩阵包含W,I,N三种字符,问相邻的字符最多能组成不重叠的WIN. 思路:比赛的时候没有发现是网络流,,居然一度以为是二分图匹配,,写了一下 ...

  8. Python(六) Python 函数

    一.认识函数 help(方法名字)  help(round) 1.功能性 2.隐藏细节 3.避免编写重复的代码 4.组织代码 自定义函数 二.函数的定义及运行特点 # 递归 def sum_num(n ...

  9. selenium+python 安装使用

    一.序言 selenium官网selenium简单教程selenium完整教程python基础教程 二.安装python3 https://www.python.org/downloads/relea ...

  10. 关于vs2012/2013的C编译器生成的exe的向后兼容xp的问题

    问题一:编译出来的exe在xp上面运行提示"不是有效的win32应用程序" 在vs2012/2013版本里面,其自带的c编译器cl.exe,若直接使用cl a.c编译,那么生成出来 ...