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. win7 ssd评分降为5.9的诡异问题解决方法

    某一天偶然发现win7的系统评分里面,磁盘一项由之前的7.9降到5.9了,SSD早听说会有降速的问题,但无论如何降,也不至于被降到5.9分这一机械硬盘普遍的分数. 百度搜了,Google搜了,中文搜了 ...

  2. P2420 让我们异或吧(树链剖分)

    题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中-xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...

  3. 【codeforces 821E】Okabe and El Psy Kongroo

    [题目链接]:http://codeforces.com/problemset/problem/821/E [题意] 一开始位于(0,0)的位置; 然后你每次可以往右上,右,右下3走一步; (x+1, ...

  4. 火狐不支持innerText属性,只支持innerHTML属性

    做的一个js的小程序放到火狐上用不了了,原因是innerText不是标准属性,换成innerHTML属性就好,但是可能需要把html标签给去掉

  5. HDU 5386 Cover(模拟)

    Cover Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Subm ...

  6. JAVA并发-内置锁和ThreadLocal

    上一篇博客讲过,当多个线程访问共享的可变变量的时候,可以使用锁来进行线程同步.那么如果线程安全性存在的3个前提条件不同时存在的话,自然就不需要考虑线程安全性了.或者说如果我们能够将某个共享变量变为局部 ...

  7. zzulioj--1816--矩形(好题数学)

    1816: 矩形 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 88  Solved: 24 SubmitStatusWeb Board Descri ...

  8. Weka中数据挖掘与机器学习系列之Weka3.7和3.9不同版本共存(七)

    不多说,直接上干货! 为什么,我要写此博客,原因是(以下,我是weka3.7.8) 以下是,weka3.7.8的安装版本. Weka中数据挖掘与机器学习系列之Weka系统安装(四) 基于此,我安装最新 ...

  9. 洛谷P1586 四方定理

    题目描述 四方定理是众所周知的:任意一个正整数nn ,可以分解为不超过四个整数的平方和.例如:25=1^{2}+2^{2}+2^{2}+4^{2}25=12+22+22+42 ,当然还有其他的分解方案 ...

  10. Vuejs2.0构建一个彩票查询WebAPP(1)

    说明:本人也是刚接触VUE.js,作为一个学习笔记,旨在与初学者共同学习.其中编程语法错误或者写作水平刺眼,还望轻喷. 使用工具:Visual Studio Code.技术栈为vue2+vuex+ax ...