A Bug‘s Life

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
3 3
1 2
2 3
1 3
1与2换位,代表1,2不同性别
2与3换位,代表2, 3不同性别,由1,2不同性别推出:1和3是相同性别
故1与3换位子有毛病的

思路

这个思路和我写的食物链是一个思路
有n个虫子
我们就开个a[2n+1](正常数组还要开大些,这儿是为了方便举例子)
分两个部落,分别为[1,n]和[n+1,2n]
这里虫1有两个身份,分别为1,n+1;
若为异性这数组a中1到n(包括1和n,下面就写a[1,n])指向数字[n+1,2n];
例如:同一队伍中有:1,3,n+2,n+4,则,1和3为同性,n+2和n+4(即2和4)为同性,而(1,3)和(2,4)为异性

代码

#include "stdio.h"
#include "string.h"
int a[999999];
int n,q=0;
void init()
{
    
    
	int i;
	for(i=1;i<=n*2;i++)
		a[i]=i;
}
int find(int x)
{
    
    
	if(x==a[x]) return x;
	else return a[x]=find(a[x]);
}
void fint(int x,int y)
{
    
    
	a[find(x)]=find(y);
}
int main()
{
    
    
	int i,m,j,k,l,n1,n2;
	scanf("%d",&n1);
	for(i=1;i<=n1;i++)
	{
    
    
		k=0;
		scanf("%d %d",&n,&m);
		init();
		while(m--)
		{
    
    
			int m1,m2,k1,k2;
			scanf("%d %d",&m1,&m2);
			if(k)
				continue;
			k1=find(m1);
			k2=find(m2);
			if(k1==k2)
			{
    
    
				k=1;
				continue;
			}	
			else
			{
    
    
				fint(m1,m2+n);//m1指向m2+n(即m2)
				fint(m2,m1+n);//m2指向m1+n(即m1)
			}
		}
		printf("Scenario #%d:\n",i);
		if(k)
		printf("Suspicious bugs found!\n");
		else
		printf("No suspicious bugs found!\n");
		if(i!=n1)
		printf("\n");
	}
} 

猜你喜欢

转载自blog.csdn.net/weixin_53623850/article/details/116739100