您的位置:首页 > 其它

POJ 2492 虫子交配

2016-07-08 09:32 218 查看
A Bug’s Life

Time Limit: 10000MS Memory Limit: 65536K

Total Submissions: 33828 Accepted: 11077

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!

Hint

Huge input,scanf is recommended.

Source

TUD Programming Contest 2005, Darmstadt, Germany

【分析】

这道题用补集做。

做的我欲仙欲死

我要告诉大家

能用flag不要用break

pity

【代码】

//poj 2492
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
int T,n,m;
int father[2000001];
int find(int x)
{
if(x==father[x]) return x;
father[x]=find(father[x]);
return father[x];
}
int main()
{
int i,j,k,x,y;
bool flag;
scanf("%d",&T);
fo(k,1,T)
{
flag=0;
scanf("%d%d",&n,&m);
for(i=1;i<=n+n;i++)
father[i]=i;
fo(i,1,m)
{
scanf("%d%d",&x,&y);
int r1=find(x);
int r2=find(y);
if(r1==r2) flag=1;
father[r1]=find(y+n);
father[r2]=find(x+n);
}
if(flag) printf("Scenario #%d:\nSuspicious bugs found!\n\n",k);
else printf("Scenario #%d:\nNo suspicious bugs found!\n\n",k);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  并查集