A Bug's Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 28703   Accepted: 9350

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

TUD Programming Contest 2005, Darmstadt, Germany
题目大意:给出n条虫子。m个配对(男<->女)的情况。问数据有没有错误(出现同性配对)?
也就是说每一个给出的ab在不同的阵营,假设查询出现了在同一阵营就出现了错误。
c数组记录属于某一个阵营。d数组记录它所属的阵营的敌对阵营。
对每一组数据出现后。更新两个数组,注意:
假设a属于x,b属于y,那么应该讲d[y]归并到x中,y归并到d[x]中
 
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int c[2100] , d[2100] ;
int find1(int a)
{
if( c[a] != a )
{
c[a] = find1(c[a]) ;
d[a] = d[ c[a] ] ;
}
return c[a] ;
}
int main()
{
int t , tt , i , j , n , m , a , b , flag ;
scanf("%d", &t);
for(tt = 1 ; tt <= t ; tt++)
{
scanf("%d %d", &n, &m);
for(i = 1 ; i <= n ; i++)
c[i] = i ;
memset(d,-1,sizeof(d));
flag = 0 ;
while(m--)
{
scanf("%d %d", &a, &b);
if( flag ) continue ;
int x , y ;
x = find1(a) ; y = find1(b) ;
if(x == y)
flag = 1 ;
else if( d[x] == -1 && d[y] == -1 )
{
d[x] = y ; d[y] = x ;
}
else if( d[x] != -1 )
{
c[y] = d[x] ;
if( d[y] != -1 )
{
int xx = find1(d[y]);
c[xx] = x ;
d[xx] = y ;
}
d[y] = x ;
}
else
{
c[x] = d[y] ;
if( d[x] != -1 )
{
int yy = find1(d[x]);
c[yy] = y ;
d[yy] = x ;
}
d[x] = y ;
}
}
printf("Scenario #%d:\n", tt);
if( flag )
printf("Suspicious bugs found!\n\n");
else
printf("No suspicious bugs found!\n\n");
}
return 0;
}

poj2492--A Bug&#39;s Life(并查集变形)的更多相关文章

  1. poj2492 A Bug's Life【并查集】

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

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

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

  3. hdu1829 A Bug&#39;s Life(并查集)

    开两个并查集.然后合并的时候要合并两次.这样在合并之前推断是否冲突,假设不冲突就进行合并,否则不须要继续合并. #include<cstdio> #include<cstdlib&g ...

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

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

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

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

  6. A Bug's Life(加权并查集)

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

  7. POJ_2492 A Bug's Life 【并查集】

    一.题面 POJ2492 二.分析 并查集判断类别的题目感觉套路都差不多. 还是先判断在不在一个集合里,在一个集合里才能判断是否同类. 若不在一个集合里则需要将这两个点联系起来. 关于联系起来后关系的 ...

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

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

  9. HDU-1829 A Bug's Life。并查集构造,与POJ1709异曲同工!

    A Bug's Life                                                     Find them, Catch them 都是并查集构造的题,不久前 ...

随机推荐

  1. Linux--对文件夹下的配置文件批量改动IP

    sed -i 's/10.1.1.1/10.1.1.2/g' `grep -ir 10.1.1.1 * |grep -E '.xml:|.cfg:|.ini:|.wsdl:|.properties:' ...

  2. Linux less命令简介

    less命令可以对文件或其它输出进行分页显示,与moe命令相似,但是比more命令要强大许多. 在 less 中导航命令类似于 vi,如下: 1 搜索 当使用命令 less file-name 打开一 ...

  3. hdoj--1162--Eddy's picture(最小生成树)

    Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. Oracle 常见的33个等待事件

    一. 等待事件的相关知识: 1.1 等待事件主要可以分为两类,即空闲(IDLE)等待事件和非空闲(NON-IDLE)等待事件. 1). 空闲等待事件指Oracle正等待某种工作,在诊断和优化数据库的时 ...

  5. Session会在浏览器关闭后消失吗?

    转  http://blog.csdn.net/rongwenbin/article/details/51784310 Cookie的两种类型   在项目开发中我们时常将需要在客户端(浏览器)缓存的数 ...

  6. 如何版本化你的API?--转

    原文地址:http://www.infoq.com/cn/news/2017/09/How-versioning-API 如何版本化API需要考虑各种实际业务场景,但是一个完备的API应该是: 和客户 ...

  7. 看似简单!解读C#程序员最易犯的7大错误

    编程时犯错是必然的,即使是一个很小的错误也可能会导致昂贵的代价,聪明的人善于从错误中汲取教训,尽量不再重复犯错,在这篇文章中,我将重点介绍C#开发人员最容易犯的7个错误. 格式化字符串 在C#编程中, ...

  8. Memcache使用场景

    session //php文件中 ini_set("session.save_handler", "memcache"); ini_set("sess ...

  9. SQL纯手写创建数据库到表内内容

    建表啥的只点点鼠标,太外行了,不如来看看我的纯手写,让表从无到有一系列:还有存储过程临时表,不间断的重排序: 一:建数据库 create Database Show on primary ( name ...

  10. RePlace函数

    <html><body> <script type="text/javascript"> var str="Visit qq1!&qu ...