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.
 
题目大意:有N个虫子,M个关系,判断这些虫子的关系(即性取向)是否正确
      按照样例输出。
解题思路:很明显用并查集比较好处理
     处理时候有一个就是保存深度的数组ra[Maxn],0表示虫子是同性,1表示虫子是异性
 
下面就闲话少叙,代码走起
 
/*之前一直没搞懂为什么ra这个数组要取余运算原来是用0
表示同类,1表示不同类,所以下面的21还有30行会取模*/
#include<iostream>
#include<cstdio>
using namespace std;
const int Maxn=2005;
int pa[Maxn],ra[Maxn];//用来存储父节点还有关系
void init(int n)
{
for(int i=1;i<=n;i++)
{
pa[i]=i;
ra[i]=0;
}
}
int find_set(int x)
{
int t=pa[x];
if(pa[x]==x) return x;
pa[x]=find_set(t);
ra[x]=(ra[x]+ra[t])%2;
return pa[x];
}
void union_set(int x,int y)
{
int fx=find_set(x);
int fy=find_set(y);
if(fx==fy) return;
pa[fx]=fy;
ra[fx]=(ra[x]+1+ra[y])%2;
}
int main()
{
int T;
int num,ncase;
int x,y;
scanf("%d",&T);
// {
for(int i=1;i<=T;i++)
{
bool flag=true;
scanf("%d%d",&num,&ncase);
init(num);
while(ncase--)
{
scanf("%d%d",&x,&y);
if(find_set(x)==find_set(y))
{
if(ra[x]==ra[y])
{
flag=false;
continue;
}
}
else
{
union_set(x,y);
}
}
printf("Scenario #%d:\n", i);
if(flag) printf("No suspicious bugs found!\n");
else printf("Suspicious bugs found!\n");
printf("\n");
}
// }
return 0;
}

并查集--poj 2492的更多相关文章

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

  2. [并查集] POJ 1703 Find them, Catch them

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: ...

  3. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  4. [并查集] POJ 1182 食物链

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66294   Accepted: 19539 Description ...

  5. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  6. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network

    题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p   代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...

  7. 并查集 POJ 1988

    #include <cstdio> #define N 30010 int pa[N]; //parent int siz_tree[N]; //size of tree int d[N] ...

  8. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  9. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. Technocup 2017 - Elimination Round 1 (Unofficially Open for Everyone, Rated for Div. 2) B

    Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. ...

  2. 转 PHP 正则表达式 以及案例

    2.Perl兼容的语法扩充 Perl兼容的正则表达式的模式类似于Perl中的语法,表达式必须包含在定界符中,除数字.字母.反斜线外的任何字符都可以作为定界符.例如,表达式’/^(?i)php[34]/ ...

  3. 18113 Secret Book of Kungfu 按位DFS

    http://acm.scau.edu.cn:8000/uoj/mainMenu.html 18113 Secret Book of Kungfu 该题有题解 时间限制:1000MS  内存限制:65 ...

  4. Python相对导入导致SystemError的解决方案(译)

    原文出处: http://stackoverflow.com/   译文出处:yibohu1899 这个问题是如何解决在相对导入的时候,如果出现’System Error’的时候的解决方案.顺带一提, ...

  5. centos7安装iptables

    使用CentOS 7时发现使用iptables防火墙时提示错误Unit iptables.service failed to load,意思是防火墙运行启动失败了,那么要如何处理呢.   一直用Cen ...

  6. Unity AssetBundle笔记

    1.入门: Resources:表示U3D自动将资源打成一个AssetBundle包,所有放在Resources下的文件夹都会打成一个AssetBundle包,资源非常大,Resources文件夹在真 ...

  7. WebService_Demo

    简述 使用IDEA开发webservice服务,从零开始一步一步指引你. 服务端开发 首先创建一个webservice项目,如下图 创建完项目后idea会帮我们创建一个类,helloword,我们把它 ...

  8. this的三个要点

    1.this的指向是什么? 指向对象 2.this可以书写在哪里? 可以写在全局,也可以写在函数里 三种写在函数里的方式: 2.1  this可变 function f() { this.name = ...

  9. Ubuntu16.04 + cuda8.0 + GTX1080安装教程

    1. 安装Ubuntu16.04 不考虑双系统,直接安装 Ubuntu16.04,从 ubuntu官方 下载64位版本: ubuntu-16.04-desktop-amd64.iso . 在MAC下制 ...

  10. SQL server 数据库基础语句 查询语句

    这一章要学习查询语句 我看car这一数据 我们就开始打上 select  *from car 条件修改 update 表名 set 列名1=值1 where 列名2=值2   //当列名2=值2时   ...