题目:
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.
2 3 3 1 2 2 3 1 3 4 2 1 2 3 4Sample Output
Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found!Hint
题意:emmmm给出哪两个可以发生性行为,判断是否有同性恋
分析:食物链简单版本,,只有两个分类,用2*n数组储存,分成两堆,之后判断a很a+n是否是在一块;
代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int f[6005];
int t,n,m;
void intt()
{
for(int i=1;i<=n*2;i++)
f[i]=i;
}
int getf(int v)
{
if(f[v]==v) return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}
bool same(int a,int b)
{
return getf(a)==getf(b);
}
void unit(int a,int b)
{
int t1,t2;
t1=getf(a);
t2=getf(b);
if(t1!=t2)
f[t2]=t1;
return ;
}
int main()
{
int i,a,b,ans,casee=0;
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%d %d",&n,&m);
intt();
for(i=1;i<=m;i++)
{
scanf("%d %d",&a,&b);
unit(a,b+n);
unit(b,a+n);
if(same(a,a+n)||same(b,b+n))
ans=1;
}
if(ans!=0)
printf("Scenario #%d: \nSuspicious bugs found!\n",++casee);
else
printf("Scenario #%d: \nNo suspicious bugs found!\n",++casee);
printf("\n");
}
return 0;
}