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;
}

你可能感兴趣的:(数据结构,并查集)