POJ 2492 A Bug's Life (bug们被悲剧了)

这个时代,是个雷人的时代。凤姐的出现,让我们对自己的审美观产生了怀疑;
著姐的降临,使我们对自己的性取向做出了更正。而如今,bug们被同性恋了,这直接就是商人们为了扩大HS产业的炒作。
哪一天,也许你也被变态了,被脑残了。。。

Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 13867 Accepted: 4450

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 exp eriment, 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!
#include
#define M 2002
//并查集,用revsex[i]记录每次的与i相对的那个性别的bug,如果一只bug和两个性别的bug都发生了关系,他们的恋情就要被公开了。
int bin[M],rank[M],revsex[M];
void MakeSet(int n)//初始化
{
	int i;
	for(i=1;i<=n;i++)
	{
		bin[i]=i;
		revsex[i]=0;
		rank[i]=0;
	}
}
int find(int x)//查
{
	if(bin[x]!=x)
		bin[x]=find(bin[x]);
	return bin[x];
}
void Union(int x,int y)//并
{
	x=find(x);
	y=find(y);
	if(x==y)return;
	if(rank[x]>rank[y])
		bin[y]=x;
	else 
	{
		if(rank[x]==rank[y])
			rank[y]++;
		bin[x]=y;
	}
}
int main()
{
	int flag,T,k,i,n,x,y,m;
	scanf("%d",&T);
	for(k=1;k<=T;k++)
	{
		flag=0;
		scanf("%d%d",&n,&m);
		MakeSet(n);
		while(m--)
		{
			scanf("%d%d",&x,&y);
			if(flag)continue;
			if(find(x)==find(y))
				flag=1;
			else 
			{
				if(revsex[x]!=0)
				{
					Union(revsex[x],y);
				}
				else revsex[x]=y;
				if(revsex[y]!=0)
				{
					Union(revsex[y],x);
				}
				else revsex[y]=x;
			}
			
		}
		printf("Scenario #%d:/n",k);
		if(flag)
			printf("Suspicious bugs found!/n/n");
		else printf("No suspicious bugs found!/n/n");
	}
	return 0;
}

你可能感兴趣的:(转载ing)