A Bug's Life HDU - 1829--种类并查集

题目

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!

分析

种类并查集,用group[ ]数组表示当前节点与根节点的关系,1为异性,0为同性。

如果a和b交配,则unionn(a,b)

出现同集合且为同性,则为异常

三个函数

init()

初始化函数,与基础并查集类似,pre[ i ]=i,这个不变,多了一个group[ i ]=0,表示自己与自己同性别。

unionn()

首先得更新根节点。假设a的根节点为x,b的根节点为y,那么a和x的关系为group[a],b和y的关系为group[b],那么x和y是什么关系呢?因为a和x的关系为group[a],a和b为异性,所以x和b的关系就是group[a]+1,那么又因为b和y的关系为group[b],所以x和y的关系为(rank[a]+rank[b]+1)%2。

find()

根节点更新了,子树还没更新。子树的更新在find函数中,在unionn函数中立马更新是没有必要的,因为有些节点可能用不到。假设a为某个未更新的节点,x为旧的根节点,w为新的根节点。未更新的pre[a]指向旧节点x,根节点pre[x]指向新根w,a和x的关系为group[a],x和w的关系为group[x],所以a和w的关系为(rank[a]+rank[pre[a]])%2。

代码

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int N=2005;
int n,m,bug;
int pre[N],group[N];
void init()
{
	for(int i=0;i<N;i++)
	{
		pre[i]=i;
		group[i]=0;
	}
}
int find(int x)
{
	if(x!=pre[x])
	{
		int px=find(pre[x]);
		group[x]=(group[x]+group[pre[x]])%2;
		pre[x]=px;
	}
	return pre[x];
}
void unionn(int x,int y)
{
	int r1=find(x);
	int r2=find(y);
	if(r1==r2)
	{
		if(group[x]==group[y])
			bug=1;
	}
	else
	{
		pre[r1]=r2;
		group[r1]=(group[x]+group[y]+1)%2;
	}
}
int main()
{
	int t,cas=1;
	scanf("%d",&t);
	while(t--)
	{
		bug=0;
		scanf("%d%d",&n,&m);
		init();
		for(int i=0;i<m;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			if(bug)
				continue;
			unionn(a,b);
		}
		printf("Scenario #%d:\n",cas++);
		if(bug)
			printf("Suspicious bugs found!\n");
		else
			printf("No suspicious bugs found!\n");
		printf("\n");
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值