A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15767 Accepted Submission(s): 5163
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.
给出许多对性别不同的组合,问有无同性恋,每输入一组x,y,为异性,那么x+n与y应该属于同一性别,y+n与x应该为同一性别,每次检测一下x与x+n,y与y+n的根节点是否相同即可,相同则意味着存在同性恋。
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<cstdio>
using namespace std;
int pre[2222222];
int flag=0;
int city,road;
int find(int x)
{
if(x==pre[x])return x;
pre[x]=find(pre[x]);
return pre[x];
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
pre[fy]=fx;
}
int main(){
int a,b;
int T;
cin>>T;
int acse=0;
while(T--)
{
acse++;
cin>>city>>road;
int i;
for(i=0;i<=city*2;i++)
pre[i]=i;
flag=0;
for(i=1;i<=road;i++)
{
scanf("%d%d",&a,&b);
merge(a,b+city);
merge(b,a+city);
if(find(a)==find(a+city)||find(b)==find(b+city))
flag=1;
}
cout<<"Scenario #"<<acse<<":"<<endl;
if(flag)cout<<"Suspicious bugs found!"<<endl<<endl;
else
{
cout<<"No suspicious bugs found!"<<endl<<endl;
}
}
return 0;
}