A Bug's Life

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 28211   Accepted: 9177

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.
 
题解:种类并查集,假设之前输入的两个放在一起的虫子不是同性恋,那么他们就是互为异性,分别将他们加入两个不同的集合,每次输入新的一组虫子时,先查询他们在不在同一集合,若在,则为同性恋,否则分别将两个虫子所在的集合与其已存过的或者根节点的异性所在的集合合并,直到最后。
 
AC代码:
 
 #include <cstdio>
#include <cstring> const int LEN = ; int uset[LEN];
int opp[LEN]; //代表每个虫子的配对异性
int rank[LEN];
int n; void makeset() //初始化并查集
{
for(int i = ; i <= n; i++)
uset[i] = i;
memset(opp, , sizeof(opp));
memset(rank, , sizeof(rank));
} int findset(int x)
{
if (x == uset[x])
return x;
else
uset[x] = findset(uset[x]);
return uset[x];
} void unionset(int x, int y)
{
if ((x = findset(x)) == (y = findset(y)))
return;
if (rank[x] > rank[y]) //按秩合并
uset[y] = x;
else{
uset[x] = y;
if (rank[x] == rank[y])
++rank[y];
}
} int main()
{
int T;
scanf("%d", &T);
for(int cnt = ; cnt <= T; cnt++){
int m;
scanf("%d %d", &n, &m);
makeset();
int f = ;
for(int i = ; i < m; i++){
int x, y;
scanf("%d %d", &x, &y);
if (f)
continue;
if (findset(x) == findset(y)){ //如果这两个虫子在同一个集合里找到 就是同性恋
f = ;
continue;
}
if (opp[x] == && opp[y] == ){ //代表两个虫子都没被分类过,储存下每个虫子的异性
opp[x] = y;
opp[y] = x;
}
else if (opp[x] == ){ //如果x没被分类过,将它的异性记录为y,并将它加入y的异性所在的集合
opp[x] = y;
unionset(x, opp[y]);
}
else if (opp[y] == ){ //同上
opp[y] = x;
unionset(y, opp[x]);
}
else{ //否者合并x也opp[y]所在的集合以及y与opp[x]所在的集合
unionset(x, opp[y]); unionset(y, opp[x]);
}
}
printf("Scenario #%d:\n", cnt);
if (f)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
printf("\n");
}
return ;
}

【POJ】2492 A bug's life ——种类并查集的更多相关文章

  1. POJ 2492 A Bug's Life (并查集)

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

  2. POJ 2492 A Bug's Life【并查集高级应用+类似食物链】

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

  3. POJ 2492 A Bug's Life(并查集)

    http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...

  4. POJ2492 A Bug's Life —— 种类并查集

    题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Su ...

  5. POJ 1703 Find them,Catch them ----种类并查集(经典)

    http://blog.csdn.net/freezhanacmore/article/details/8774033?reload  这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...

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

    思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...

  7. hdu1829A Bug's Life(种类并查集)

    传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...

  8. POJ 1703 Find them, Catch them(种类并查集)

    题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...

  9. hdoj 1829 A bug's life 种类并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...

随机推荐

  1. MySQL操作类的封装(PHP)

    <?php class mysql{ /** * 报错函数 * * @param string $error */ function err($error){ die("对不起,您的操 ...

  2. [Django 1.5] Django 开发学习资源链接

    jQuery : jQuery API introduction:http://api.jquery.com/ jQuery plugins: http://benalman.com/projects ...

  3. Flex中如何通过horizontalTickAligned和verticalTickAligned样式指定线图LineChart横竖方向轴心标记的例子

    原文http://blog.minidx.com/2008/12/03/1669.html 接下来的例子演示了Flex中如何通过horizontalTickAligned和verticalTickAl ...

  4. rageagainstthecage 源代码

    //头文件包含 #include <stdio.h> #include <sys/types.h> #include <sys/time.h> #include & ...

  5. 人生第一场组队赛---2014.8 zju monthly

    暑期集训中段就组了队,不过一直没机会打比赛 昨天kitkat突然发现了zju要搞月赛,我想了一下题目对于我这种渣实在是有点难,于是想到干脆打一次组队赛吧,跟队友商量了一下也同意了 12点---17点  ...

  6. paip.QQ音乐导出歌单总结

    paip.QQ音乐导出歌单总结 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net/attilax ...

  7. SQLite使用报告

    SQLite简介 SQLite是遵守ACID的关联式数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目. 不像常见的客户-服务器范例,SQLite引擎不是个程 ...

  8. DOM注意事项(八):JavaScript操作环境和垃圾收集

    一.运行环境 在约JavaScript对象或this当指向问题,念就是运行环境.即上下文环境.运行环境在JavaScript是一个非常重要的概念.由于它定义了变量或函数有权訪问的其他数据,决定了它们各 ...

  9. 使用Cloudsim实现基于多维QoS的资源调度算法之中的一个:配置Cloudsim环境

    Cloudsim是一款开源的云计算仿真软件,它继承了网格计算仿真软件Gridsim的编程模型,支持云计算的研究和开发.它是一个自足的支持数据中心.服务代理人.调度和分配策略的平台,支持大型云计算的基础 ...

  10. SQL常用函数

    SQL中常用的函数有类型转换函数.字符串函数和日期使用函数.更多具体的函数用法参见DBMS中的帮助文档. 类型转换函数 cast(值 as 类型) update 表名 set 列1=列1+cast(列 ...