HDU1829 A Bug's Life(并查集,深度记录)

A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19429    Accepted Submission(s): 6206


 

Problem 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.

 

 

Source

TUD Programming Contest 2005, Darmstadt, Germany

 入门级的并查集题目,我们可以根据节点和根节点的相对距离来判断相对性别,双数为同性,单数为异性(因为只有其奇偶有意义而具体的大小有意义所以我们选择只记录其奇偶)

这里有两个时候需要维护状态:

find时:

sex[x]=(sex[x]+sex[fa[x]])&1;节点到根节点的距离等于节点的距离到根节点的距离,加上根节点到根节点的距离(合并后的根节点发生了变化,所以要加上原根节点到新根节点的距离)

需要注意的是,每组样例完成后需要空一行(output里没说,注意一下)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define int long long
#define endl '\n'
#define sc(x) scanf("%lld",&x)

using namespace std;
int fa[2005];
int sex[2005];
void init(int n)
{
	for(int i=1;i<=n;i++)
	{
		fa[i]=i;
		sex[i]=0;
	}
}
int find(int x)
{
	if(x==fa[x]) return x;
	int t=find(fa[x]);
	sex[x]=(sex[x]+sex[fa[x]])&1;
	return fa[x]=t;
}
int merge(int x,int y)
{
	int fx=find(x),fy=find(y);
	if(fx==fy) 
	{
		if(sex[x]==sex[y])
		{
			return 0;
		}
		return 1;
	}
	fa[fx]=fy;
	sex[fx]=(sex[x]+sex[y]+1)&1;
	return 1;
}
int32_t main()
{
	int t;
	sc(t);
	int cnt=1;
	while(t--)
	{
		int n,m;
		sc(n),sc(m);
		init(n);
		int ans=1;
		for(int i=0;i<m;i++)
		{
			int a,b;
			sc(a),sc(b);
			if(!merge(a,b)) ans=0;
		}
		printf("Scenario #%lld:\n",cnt++);
		if(!ans) cout<<"Suspicious bugs found!"<<endl;
		else cout<<"No suspicious bugs found!"<<endl;
		printf("\n");
	}
	return 0;
}

 

posted @ 2018-08-06 19:22  Fly_White  阅读(121)  评论(0编辑  收藏  举报