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. Ruby学习笔记(二)——从管道读取数据

    在对文件名修改后,今天又给自己出了新的难题,想从实验结果中提取数据,并将其作为文件夹的名称.其中,比赛的主办方提供的评估算法是用perl写的,因此读取实验结果最为简单的想法自然是使用管道命令,即 ./ ...

  2. 谷歌开源可视化工具Facets,将用于人+AI协作项目研究——无非就是一个用于特征工程探索的绘图工具集,pandas可以做的

    见:http://www.infoq.com/cn/news/2017/07/goole-sight-facets-ai https://github.com/PAIR-code/facets/blo ...

  3. Java-MyBatis:MyBatis XML 文件

    ylbtech-Java-MyBatis:MyBatis XML 文件 1.返回顶部 1. Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大, ...

  4. Laravel异常处理

    Laravel异常处理 标签(空格分隔): php 自定义异常类 <?php namespace App\Exceptions; use Throwable; use Exception; cl ...

  5. Python中functools模块函数解析

    Python自带的 functools 模块提供了一些常用的高阶函数,也就是用于处理其它函数的特殊函数.换言之,就是能使用该模块对可调用对象进行处理. functools模块函数概览 functool ...

  6. 蓝牙音箱BluetoothA2dp

    package myapplication.com.mybuletooch; import android.support.v7.app.AppCompatActivity; import andro ...

  7. swift使用查阅资料备份4

    Swift - RxSwift的使用详解6(观察者2: 自定义可绑定属性) http://www.hangge.com/blog/cache/detail_1946.html extension UI ...

  8. Object-oriented programming

    Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects&qu ...

  9. 将数据内容动态添加到HTML中

    // 申明一个数组用来装遍历的元素 var li = []; //遍历元素并加载到标签中 for(var i = 0; i<navGroup.self_first_nav.length; i++ ...

  10. GDB Layout

    layout:用于分割窗口,可以一边查看代码,一边测试.主要有以下几种用法:layout src:显示源代码窗口layout asm:显示汇编窗口layout regs:显示源代码/汇编和寄存器窗口l ...