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. distance.c

    #include "stdio.h" #include "string.h" #include "math.h" #include &quo ...

  2. 启用 ASP.NET MVC 项目的 Edit and Continue

    VS 的 Edit and Continue 功能允许你在 Debug 的过程中,修改代码并且编译运行修改后的代码.对于编程阶段非常的好用,不需要你停止正在进行的 Debug,修改代码然后运行代码. ...

  3. (1)html初步--表格的使用

    用表格标签制作简历 html代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  4. css案例学习之全局声明*{} 与body{}的区别

    代码 <html> <head> <title>全局声明</title> <style type="text/css"> ...

  5. 普林斯顿大学算法课 Algorithm Part I Week 3 排序稳定性 Stability

    稳定性(Stability):先按性质A排序,再按性质B排序,性质B相同的那些项是否仍然是按性质A排序的? 一个稳定的排序,相同值的元素应仍保持相对顺序(relative order) 稳定的算法:插 ...

  6. iOS开发 使用Xcode自带的Leaks

    http://www.jianshu.com/p/0837331875f0 http://www.cnblogs.com/tanzhenblog/p/5001344.html?utm_source=t ...

  7. 可解压带中文名称文件的zip包

    package com.text.ziptest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; i ...

  8. TCP协议三次握手过程分析

    TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种标 ...

  9. asp.net BulletedList绑定数据及vs2013添加数据库文件

    首先是在网页中添加一个BulletedList控件,通过编辑项来添加显示的数据. 这是一种添加数据的方式,另一种是通过绑定数据源来实现.在此之前,要先添加一个sql server数据库: 点开右键菜单 ...

  10. MSDN地址,记录下来,以防以后使用

    MSDN在线官网:https://msdn.microsoft.com/zh-cn/default.aspx 以备学习时候使用.