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. js onclick=‘save()’ 和 onclick='return save()'

    onclick="function()" 表示只会执行 function , 但是不会传回 function 中之回传值onclick = "return functio ...

  2. HTML文档设置标记

    格式标记 1.<br> 强制换行标记,让后面的文字.图片.表格等,显示在下一行.单标记 2.<p> 换段落标记,换段落是由于多个空格和回车在HTML中会被等效为一个空格,所以H ...

  3. h5画圆

    下面一段代码是,h5的画圆,半圆,四分之一圆等效果 <!DOCTYPE html> <html lang="en"> <head> <me ...

  4. sql server 时间查询

    select CONVERT(varchar, getdate(),8 )   --获取当前时间时分14:13:59 select CONVERT(varchar, getdate(),23 )  - ...

  5. Flask 学习系列(二)---Jinjia2模板

    下面是一个jinjia2的简单模板的例子: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  6. Spring Cloud Gateway VS Zuul 比较,怎么选择?

    Spring Cloud Gateway 是 Spring Cloud Finchley 版推出来的新组件,用来代替服务网关:Zuul. 那 Spring Cloud Gateway 和 Zuul 都 ...

  7. BeanUtils 工具类

    一.BeanUtils 概述     BeanUtils 是阿帕奇提供的一套专门用于将一些数据封装到java对象中的工具类;          名词:javaBean:特定格式的java类称为java ...

  8. [原创] SOAP UI 创建SOAP工程进行接口测试

    下载及安装 1. 登录http://www.soapui.org/ 2. 鼠标移动到导航头的Downloads选项 3. 点击SOAP UI 4. 下载页面 新建项目 创建项目 1. 创建项目很简单. ...

  9. 洛谷 P1629 邮递员送信

    题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要一定的时间.这个邮递员每 ...

  10. 结合浅层高层特征的paper总结

    1.ION:在conv3.conv4.conv5和context features上分别进行roi_pooling,在channel那一维进行concat 2.Hypernet:在较浅层max_poo ...