POJ-2492-A Bug's Life(并查集)

                                                         A Bug's Life
 

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!

题意描述:

给你n个虫子和m条信息,给的两个虫子的编号认为这两个虫子是异性,判断有没有同性恋

程序代码:

#include<stdio.h>
int f[5010],n,m;
int getf(int u);
int F(int a,int b);
void merge(int u,int v);
int main()
{
	int T,i,j,k,flag,x,y,count=1;
	scanf("%d",&T);
	for(k=1;k<=T;k++)
	{
		flag=0;
		scanf("%d%d",&n,&m);
		for(i=1;i<=2*n;i++)
			f[i]=i;
		while(m--)
		{
			
			scanf("%d%d",&x,&y);
			if(F(x,y)==1||F(x+n,y+n)==1)
			{
				merge(x,y+n);
				merge(x+n,y);
			}
			else
				flag=1;
		}
		if(k!=1)
			printf("\n");
		printf("Scenario #%d:\n",count++);
        if(flag==0)
            printf("No suspicious bugs found!\n");
        else
            printf("Suspicious bugs found!\n");
        if(k==T)
        	printf("\n");
	}
	return 0;
}
int getf(int u)
{
	if(u==f[u])
		return u;
	f[u]=getf(f[u]);
	return f[u];
}
void merge(int u,int v)
{
	u=getf(u);
	v=getf(v);
	if(u!=v)
		f[v]=u;
}
int F(int a,int b)
{
	if(getf(a)!=getf(b))
		return 1;
	return 0;
}

 

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页