hdu 1829 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): 6495    Accepted Submission(s): 2126


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!

 

 

题意:是给出一些虫子的关系,判断里面有没有同性关系,比方说1,2有关系,2,3有关系,1,3有关系。这这里面一定存在搞同性的虫子。

 

#include"stdio.h"
#include"string.h"
int father[2011];
int sex[2011];
int find(int x)
{
	if(father[x]!=x)
	father[x]=find(father[x]);
	return father[x];
}
void Union(int x,int y)
{
	
	x=find(x);
	y=find(y);
	if(x!=y)	
	father[x]=y;
}
int main()
{
	int t,k,n,m,i,a,b,flag;
	scanf("%d",&t);
	for(k=1;k<=t;k++)
	{
		scanf("%d%d",&n,&m);
			for(i=1;i<=n;i++)
				father[i]=i;
		memset(sex,0,sizeof(sex));
		flag=0;
		while(m--)
		{
			scanf("%d%d",&a,&b);
			if(flag)
				continue;
		//	printf("%d %d\n",find(a),find(b));
			if(find(a)==find(b))//如果a,b在一个集合出现,证明是同性
			{
				flag=1;
				continue;
			}	
			if(sex[a]==0)//判断是否是第一次出现
				sex[a]=b;//a与b是异性
			else	
				Union(sex[a],b);//sex[a]与a是异性,a与b是异性,则sex[a]与b是同性
			if(sex[b]==0)	//同理
				sex[b]=a;
			else	
				Union(sex[b],a);
		}
		
		   printf("Scenario #%d:\n",k);
		if(flag)
			printf("Suspicious bugs found!");
		else
			printf("No suspicious bugs found!");
		printf("\n\n");
	}
	return 0;
}

谢谢阅读!

你可能感兴趣的:(并查集)