【题目】
1437: A Bug's Life
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 112 Solved: 49
[Submit][Status][Web Board]
Description
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. 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 1, and up to 2000) and the number of interactions (up to 5000) 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 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
Suspicious bugs found!
No suspicious bugs found!
【题意】
输入一对恋人,判断是否一定存在同性恋。
【思路】
用pre[]存父节点,用r[]存性别。初始化性别为0,假设输入的恋人性别不同,如果父节点不同说明没有出现过,未确定性别,则将其中一个改变性别;若父节点相同且性别相同,则说明一定存在同性恋。
【代码】
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <map>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define og(i,a,b) for(int i=a;i>=b;i--)
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
int pre[2005],r[2005],f;
int find(int x)
{
if(x==pre[x]) return x;
int t=find(pre[x]);
r[x]=(r[x]+r[pre[x]])%2;
return pre[x]=t;
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx==fy)
{
if(r[x]==r[y]) f=1;
return;
}
pre[fx]=fy;
r[fx]=(r[x]+r[y]+1)%2;
}
main()
{
int t,n,m,a,b;
scanf("%d",&t);
while(t--)
{
f=0;
scanf("%d%d",&n,&m);
go(i,1,n) pre[i]=i,r[i]=0;
while(m--)
{
scanf("%d%d",&a,&b);
if(f) continue;
else join(a,b);
}
if(f) printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
}
}