A Bug's Life
http://poj.org/problem?id=2492
Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 12 Accepted Submission(s) : 7
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.
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!HintHuge input,scanf is recommended.
Source
TUD Programming Contest 2005, Darmstadt, Germany
题目大意:给出多个案例、输入n,m表示n只bugs,共有m组关系、紧接着m+1组关系
求是否有关系混乱(即“同性恋”)
题解:并查集、定义一个关系数组gender当输入的bug没有已存在关系时、设关系为同组输入的bug
即在并查集前加了个一个“判定”,当输入1---2时将1的关系gender[1]=2,gender[2]=1,则在输入
1--3时此时合并union(gender[1],3)即union(2,3),gender[3]=1这时可知3与2肯定是同性的
则再输入2--3时find-father。。。此时即可将同类的的合并
#include <cstdio>
int father[2001];
int gender[2001];
int Find_set(int x)
{
if(father[x]!=x)
{
father[x]=Find_set(father[x]);
}
return father[x];
}
void Union(int x,int y)
{
x=Find_set(x); //Union 中再次查找树根,可以降低数的高度
y=Find_set(y);
if(x!=y)
father[x]=y;
}
using namespace std;
int main()
{
int cases;
scanf("%d",&cases);
int t=1;
while(cases--)
{
bool find=false;
int bugs,n;
scanf("%d%d",&bugs,&n);
for(int i=1;i<=bugs;++i)
{
father[i]=i;
gender[i]=0; //性别只有两类,均初始化为0
}
for(int i=0;i<n;++i)
{
int x,y;
scanf("%d%d",&x,&y);
if(!find)
{
if(Find_set(x)==Find_set(y))
{
find=true;
continue;
}
if(gender[x]==0)
gender[x]=y; //gender[x]用于存储对方的类别
else
Union(gender[x],y); //与x相反的类及与y相同的类,将其合并
if(gender[y]==0)
gender[y]=x;
else
Union(x,gender[y]);
}
}
printf("Scenario #%d:\n",t);
if(find)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
t++;
printf("\n");
}
}