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.
 
【题意】:Hopper 在研究某种稀有虫子的性行为。他假设虫子们有两种不同的性别,而且它们只跟异性发生关系。
在他的试验里,每个虫子和它的性行为都很容易辨认,因为它们的背后印着号码。
给出一些虫子的性行为,确定是否有同性恋的虫子能推翻这个假设。某物种n只,m个描述,表示x,y为异性,判断是否有同性交配。如果发现存在同性交配输出bugs found.
【分析】:

理论上这一题和HDU-1829应该都有两种方法,一个是设置两个并查集,将冲突的两个虫子合并入两个不同的并查集中,在合并的过程中如果发现合并冲突则表明出现问题,直接记录。
第二种方法则是同时记录偏移量和合并元素,倘若出现新输入的两个元素位于同一集合且偏移量相同,表明该两个元素与同一个元素都有交集,加上他们自身也有交集,表明他们同性恋,存在bug。
【代码】:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string> using namespace std;
const int maxn = ;
int fa[maxn],r[maxn];
int n,m,a,b;
void init()
{
for(int i=;i<=n;i++)
{
fa[i]=i;
r[i]=;
}
} int find(int x)
{
if(fa[x]==x) return x;
else
{
int rt=find(fa[x]);
// fa[x]=find(fa[x]);
r[x]=r[x]^r[fa[x]];
return fa[x]=rt;
}
return fa[x];
} void join(int x,int y)
{
int fx=find(x);
int fy=find(y);
fa[fx]=fy;
r[fx]= (!(r[y]^r[x]));
/*fa[fy]=fx;
r[fy]=(1+r[y]+r[x])%2;*/
} int main()
{
int t,flag;
int cas=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();//注意初始化!
flag=;
for(int i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
if(find(a)!=find(b))//根不同就合并
{
join(a,b);
}
else
{
if(r[a]==r[b])//r[x]=r[y]可推出x和y同性,那么就说明存在bug。详见代码:
flag=;
}
if(flag) continue;
} printf("Scenario #%d:\n",++cas); if(flag)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n"); printf("\n");
}
}

位运算版类别偏移

POJ 2492 A Bug's Life【并查集高级应用+类似食物链】的更多相关文章

  1. nyoj 209 + poj 2492 A Bug's Life (并查集)

    A Bug's Life 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Background  Professor Hopper is researching th ...

  2. POJ 2492 A Bug's Life 并查集的应用

    题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...

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

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

  4. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

  5. A Bug's Life POJ - 2492 (种类或带权并查集)

    这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...

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

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

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

  8. hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  9. poj 2492 A Bug's Life 二分图染色 || 种类并查集

    题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...

随机推荐

  1. NIO Q&A(持续补充。。。。)

    Q:NIO是非阻塞的.但调用的selector.select()方法会阻塞.这和NIO非阻塞岂不是矛盾了? A:非阻塞指的是 IO 事件本身不阻塞,但是获取 IO 事件的 select 方法是需要阻塞 ...

  2. NOIP2018复赛 游记

    Day -? 停了两个星期的课,逃了一场期中考试.随便做了点题并不知道有什么用.见不到夫人很难受. Day 0 依然没有跟学校走.据说今年合肥居然不下雨,印象里每年这个时候来到这里都是阴雨连绵……突然 ...

  3. [Leetcode] 3sum-closest 给定值,最为相近的3数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  4. 洛谷 P2529 [SHOI2001]击鼓传花 解题报告

    P2529 [SHOI2001]击鼓传花 题意:求出\(n!\)末尾最后一位非0数字 数据范围:\(n<=10^{100}\) 我们从简单的开始考虑 1.显然,\(n!\)可以被这么表示 \(n ...

  5. selenium 获取某个元素的html

    <table> <tbody id="tb-37327761306"> <tr class="sep-row"><td ...

  6. Windows2008 – Task Scheduler – ‘Action “C:\Windows\SYSTEM32\cmd.exe” with return code 1’

    Remediation Edit Task Let us make the necessary changes, which is to specify the Start folder. Here ...

  7. Java之戳中痛点 - (3)三目运算符的两个操作数类型尽量一致

    先看一个例子: package com.test; public class TernaryOperator { public static void main(String[] args) { in ...

  8. 解决在ios下不能自动播放音频的问题

    在ios下面是不允许自动播放音频这个操作的,不过我们可以使用微信提供的sdk来做到自动播放音乐的功能 wx.ready(()=>{}) 引入微信的sdk,之后在回调函数里面执行主动触发背景音乐播 ...

  9. Win7/8 绿色软件开机启动

    在查找番茄工作法PC端软件时,发现了淡高的文章win8绿色软件开机启动,试用了一下wintabs,的确好用! 另外,office软件中有一款 OFFICE tabs的插件,标签式的管理,非常方便快捷, ...

  10. 【Foreign】猜测 [费用流]

    猜测 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 3 1 1 1 2 2 1 Samp ...