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.

假设虫子只和异性交流 现在告诉你交流的虫子号码 问这个假设正确不正确

现在发现并查集就是找节点和父节点的关系 rank数组就是x对fx的关系 有时候不考虑方向比如这一题

像这种题目更新的时候 fx的rank应该是 x对fx的关系加fy对y的关系加x对y的关系

输出之间要空行!

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 2005;
int t, n, m;
int ran[maxn], parent[maxn]; void init(int n)
{
for(int i = 0; i < n; i++){
ran[i] = 0;
parent[i] = i;
}
} int fin(int x)
{
if(x == parent[x]) return x; int t = parent[x];
parent[x] = fin(parent[x]);
ran[x] = (ran[x] + ran[t]) % 2;
return parent[x];
} void mer(int x, int y)
{
int tx = fin(x);
int ty = fin(y); if(tx != ty){
parent[tx] = ty;
ran[tx] = (ran[x] + 1 + ran[y]) % 2;
} } int main()
{
scanf("%d", &t);
for(int i = 1; i <= t; i++){
scanf("%d%d", &n, &m);
init(n);
bool flag = true;
for(int j = 0; j < m; j++){
int a, b;
scanf("%d%d", &a, &b);
if(fin(a) == fin(b)){
if(ran[a] == ran[b])
{
flag = false;
}
}
else{
mer(a, b);
}
} if(!flag){
printf("Scenario #%d:\nSuspicious bugs found!\n\n", i);
}
else{
printf("Scenario #%d:\nNo suspicious bugs found!\n\n", i);
}
}
return 0;
}

poj2492 A Bug's Life【并查集】的更多相关文章

  1. [poj2492]A Bug's Life(并查集+补集)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 34678   Accepted: 11339 D ...

  2. hdu 1829 A Bug's Life(并查集)

                                                                                                    A Bu ...

  3. J - A Bug's Life 并查集

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

  4. nyoj 209 + poj 2492 A Bug's Life (并查集)

    A Bug's Life 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Background  Professor Hopper is researching th ...

  5. poj 2492A Bug's Life(并查集)

    /* 目大意:输入一个数t,表示测试组数.然后每组第一行两个数字n,m,n表示有n只昆虫,编号从1—n,m表示下面要输入m行交配情况,每行两个整数,表示这两个编号的昆虫为异性,要交配. 要求统计交配过 ...

  6. 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条臭 ...

  7. POJ 2492 A Bug's Life 并查集的应用

    题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...

  8. poj2492_A Bug's Life_并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 34947   Accepted: 11459 D ...

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

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

  10. A Bug's Life____并查集

    English preparation: falsify     伪造:篡改:歪曲:证明...虚假 the sexual behavior of a rare species of bugs. 一种稀 ...

随机推荐

  1. Java实现高效的枚举元素集合

    Set是Java集合类的重要组成部分,它用来存储不能重复的对象.枚举类型也要求其枚举元素各不相同.看起来枚举类型和集合是很相似的.然而枚举类型中的元素不能随意的增加.删除,作为集合而言,枚举类型非常不 ...

  2. ios的单元測试OCUnit以及更新了之后的XCTestCase

    1.像一般创建项目的步骤一样.创建一个用于測试的项目或者打开一个待測试的项目. (oc是5.0之前所使用的測试,如今用的是XCtestCase,默认会创建一个主的測试类.曾经版本号可能非常多步骤省去) ...

  3. MacOS的多重启动工具

    在osx Lion升级到Mavericks后原有的refit(http://refit.sourceforge.net)启动管理工具就失效了,refit已经停止更新,新的分支项目时rEFInd(htt ...

  4. WebService之CXF

    一.配置环境变量(Windows系统下要重启) 1.JAVA_HOME即JDK安装路径bin上一级,java -version命令验证 2.CXF_HOME即cxf安装路径bin上一级,cxf解压包下 ...

  5. Window日志分析

    0X00 简介 0x01 基本设置 A.Windows审核策略设置 前提:开启审核策略,若日后系统出现故障.安全事故则可以查看系统的日志文件,排除故障,追查入侵者的信息等. 打开设置窗口 Window ...

  6. WAF Bypass数据库特性(Mysql探索篇)

    0x01 背景 Mysql数据库特性探索,探索能够绕过WAF的数据库特性. 0x02 测试 常见有5个位置即:   SELECT * FROM admin WHERE username = 1[位置一 ...

  7. SaltStack 批量分发文件

    这里演示如何将 salt-master 上的文件批量分发到多台 salt-minion,步骤如下: [root@localhost ~]$ cat /srv/salt/top.sls # 先定义入口配 ...

  8. Selenium 节点交互

    Selenium 可以驱动浏览器来执行一些操作,也就是说可以让浏览器模拟执行一些动作 常见方法:输入文字时用 send_keys() 方法,清空文字时用 clear() 方法,点击按钮时用 click ...

  9. gitlab 使用现有 nginx 服务器

    gitlab 安装自带 nginx,如果想利用原有 nginx,可按如下操作: 8.0 版本 socket 文件位置有变动,感谢评论区的同学. nginx 增加虚拟主机配置 # gitlab sock ...

  10. URI跳转方式地图导航的代码实践

    本文转载至 http://adad184.com/2015/08/11/practice-in-mapview-navigation-with-URI/ 前言 之前介绍了我正在做的是一款定位主打的应用 ...