您的位置:首页 > Web前端

poj 2492 A Bug's Life

2016-12-29 22:35 459 查看
A Bug's Life

Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 35310 Accepted: 11588
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


提示

题意:

给你n(1<=n<=2000)个臭虫相互间的m(m最大为1000000)条信息,且这m条信息表示他们的性别为异性,如果存在信息有误,输出"Suspicious bugs found!",否则输出"No suspicious bugs found!"。

如样例一:1与2,2与3为异性,那么1与3为同性,但第3条信息与实际矛盾。

思路:

poj1703类似,另外开一个数组表示与父亲节点的关系,不等表示异性,相等表示同性。当父亲节点不同时合并集合,否则看它们的关系是什么样的。

当然只要存在一次就可以对后面输入的数据不作处理,但还是要等把数据输完才行。

示例程序

Source Code

Problem: 2492 Code Length: 1383B
Memory: 408K Time: 829MS
Language: GCC Result: Accepted
#include <stdio.h>
int a[100000],relation[100000];
int find(int x)
{
int pos;
if(a[x]==x)
{
return x;
}
pos=a[x];
a[x]=find(a[x]);
if(relation[x]==relation[pos])
{
relation[x]=0;
}
else
{
relation[x]=1;
}
return a[x];
}
int main()
{
int n,i,t,i1,x,y,fx,fy,m,flag;
scanf("%d",&t);
for(i=1;t>=i;i++)
{
scanf("%d %d",&n,&m);
flag=0;
for(i1=0;n>i1;i1++)
{
a[i1]=i1;
relation[i1]=0;
}
for(i1=1;m>=i1;i1++)
{
scanf("%d %d",&x,&y);
fx=find(x);
fy=find(y);
if(flag==1)
{
continue;
}
if(fx!=fy)
{
a[fx]=fy;
if(relation[x]==relation[y])
{
relation[fx]=1;
}
else
{
relation[fx]=0;
}
}
else if(relation[x]==relation[y])
{
flag=1;
}
}
printf("Scenario #%d:\n",i);
if(flag==1)
{
printf("Suspicious bugs found!\n");
}
else
{
printf("No suspicious bugs found!\n");
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: