天天看點

poj2492(并查集)

A Bug's Life

Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 24298 Accepted: 7906

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!      
本題可以用并查集做;      
對于任意一組新關系(a,b),若a,b性别相同,則出錯标記;否則插入該組新關系      
par[a]存放a的父親節點,relation[a]存放a與其父親節點的關系(其中0表示同性,1表示異性)      
初始化時par[a]=a,relation[a]=0;      
int Get_par(int a)      
壓縮路徑,建立每個節點與根節點的關系
{
 if(par[a]==a)
  return par[a];
 int pa=par[a];
 par[a]=Get_par(par[a]);
 relation[a]=(relation[a]+relation[pa])%2;//通過a與其父親結點,其父親結點與根節點的關系來确定a與根節點的關系,注意順序不能颠倒
 return par[a];
}      
void Merge(int a,int b)      
//連接配接a,b,保證根節點關系不斷
{
 int pa,pb;
 pa=Get_par(a);
 pb=Get_par(b);
 par[pa]=pb;
 relation[pa]=(1+relation[a]+relation[b])%2;
}      
#include<iostream>
#include<cstdio>
using namespace std;
int const MAX=2000+20;


int n,m;
int par[MAX];
int relation[MAX];

int Get_par(int a)
{
	if(par[a]==a)
		return par[a];
	int pa=par[a];
	par[a]=Get_par(par[a]);
	relation[a]=(relation[a]+relation[pa])%2;
	return par[a];
}


void Merge(int a,int b)
{
	int pa,pb;
	pa=Get_par(a);
	pb=Get_par(b);
	par[pa]=pb;
	relation[pa]=(1+relation[a]+relation[b])%2;
}

int main()
{
	int cas,i,tag;
	int a,b,pa,pb;
	bool flag;
	cin>>cas;
	tag=1;
	while(cas--)
	{
		flag=false;
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)
		{
			par[i]=i;
			relation[i]=0;
		}

		for(i=0;i<m;i++)
		{
			scanf("%d%d",&a,&b);
			pa=Get_par(a);
			pb=Get_par(b);
		//	cout<<pa<<"*****"<<pb<<endl;
			if(pa==pb)
			{
				if((relation[a]+relation[b])%2==0)
				{
					flag=true;
					continue;
				}
			}
				else 
					Merge(a,b);
		}
		printf("Scenario #%d:\n",tag++);
		if(flag)
			printf("Suspicious bugs found!\n\n");
		else 
			printf("No suspicious bugs found!\n\n");
	}
	return 0;
}
           

繼續閱讀