POJ_2492 A Bug‘s Life

A Bug’s Life

链接

POJ_2492 A Bug’s Life

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.

解析

本题是一道并查集题目。

但是,由于本题的集合中个元素的关系不同,所以对于一个元素,我们不仅要知道它的祖先是谁,还要知道它与祖先是异性还是同性。

我们可以开一个与祖先数组 p r e pre pre 长度一样的数组 r e a rea rea ( R e l a t i o n s h i p Relationship Relationship ) 来记录每个元素与其祖先的关系。其中,若 r e a i = 0 rea_i=0 reai=0,表示 i i i 与它的祖先 p r e i pre_i prei 是同性;若 r e a i = 1 rea_i=1 reai=1,则表示表示 i i i 与它的祖先 p r e i pre_i prei 是异性。

在做路径压缩时,我们不能直接将祖先 r r r 赋值给 p r e i pre_i prei,而是要先做一次异或运算 r e a i = r e a i ⊕ r e a p r e i rea_i=rea_i \oplus rea_{pre_i} reai=reaireaprei,求出 i i i 与其祖先的关系,再做 p r e i = r pre_i=r prei=r

题目要求我们对给出的异性关系判断真假情况。

若一对异性关系为假,首先这两个元素要有同一个祖先。若两个元素与祖先的关系均为异性或均为同性,那么两个元素的关系必然不可能是异性。只有两个元素一个与祖先互为同性,一个与祖先互为异性,这两个元素才可能互为异性。

两个元素的真实关系我们可以通过 r e a a ⊕ r e a b rea_a \oplus rea_b reaareab 求得,再将该结果与 1 1 1 异或,便可以判断数据给出的关系的真假情况。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
int t,n,m,a,b,fa,fb;
int pre[2001];//pre[i]:i的祖先 
int rea[2001];//rea[i]:i与其祖先的关系(0为同,1为异)
bool flag;
int find_root(int x)
{
	int r=x;
	if(x!=pre[x])
	{
		r=find_root(pre[x]);
		rea[x]^=rea[pre[x]];//先求与祖先的关系
		pre[x]=r;//再进行路径压缩
	}
	return r;
}
int main()
{
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		scanf("%d%d",&n,&m);
		flag=false;
		for(int j=1;j<=n;j++)
		{
			pre[j]=j;
			rea[j]=false;
		}
		for(int j=1;j<=m;j++)
		{
			scanf("%d%d",&a,&b);
			if(flag)
				continue; 
			fa=find_root(a);
			fb=find_root(b);
			if(fa==fb)
			{
				if(rea[a]^rea[b]^1)//判断数据给出的异性关系是否为真
					flag=true;
			}
			else
			{
				pre[fb]=fa;//合并两个集合
				rea[fb]=rea[a]^rea[b]^1;//
			}
		}
		printf("Scenario #%d:\n",i);
		if(flag)
			printf("Suspicious bugs found!\n\n");
		else
			printf("No suspicious bugs found!\n\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值