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;
}

最新文章

  1. 普通B/S架构模式同步请求与AJAX异步请求区别(个人理解)
  2. easyui tree折叠
  3. C# 的Brush 及相关颜色的操作 (并不是全转)
  4. Python基础:函数式编程
  5. python split()函数使用拆分字符串 将字符串转化为列表
  6. centos 安装 mysql 5.6和workbench
  7. Android布局居中的几种做法
  8. golang的连接池例子
  9. 反人类的MyEclipse之-MyEclipse设置Console字体大小
  10. JQuery ajax返回JSON时的处理方式
  11. ZJOI2010网络扩容
  12. js 验证手机号 以及身份证正则表达式
  13. openstack私有云布署实践【16.2 Ubuntu1404 只有根分区镜像制作】
  14. Delphi的Hint介绍以及用其重写气泡提示以达到好看的效果
  15. 图片布局css
  16. BZOJ 3997: [TJOI2015]组合数学 [偏序关系 DP]
  17. .NET redis cluster
  18. 解读经典《C#高级编程》泛型 页114-122.章4
  19. python之常用模块学习
  20. MySQL导入数据报 Got a packet bigger than‘max_allowed_packet’bytes 错误的解决方法

热门文章

  1. centos6.7版本下配置ssh密钥登录
  2. python学习之调试 错误捕捉及处理
  3. ECSHOP商品属性调用到任意页面方法
  4. [github][https模式下提交记住密码]
  5. java的8大排序详解
  6. Java之内部类、包及代码块
  7. 【CSS】纯css实现立体摆放图片效果
  8. 中移动TD-LTE 4G设备招标
  9. &quot;xxadmin&quot; user: No protocol specified 错误
  10. POJ 2152 Fire (树形DP,经典)