poj 2492 A Bug's Life
2016-12-29 22:35
459 查看
A Bug's Life
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;
}
相关文章推荐
- 值得前端开发者在2017学习的东西
- css3-3d转换和3D动画
- Web基础知识三之文本标记
- Web基础知识二之Html快速入门
- css响应式布局RWD
- 表单
- caffe若干项目实战提高
- JS 小数的常用处理方法
- jsp+js实现有刷新的用户名是否存在的检测功能
- web前端性能调优(一)
- js parsefloat
- jQuery——内容与表单选择器
- 雷锋推到雷峰塔,Java implements Javascript。
- undefined reference to `pthread_create' collect2: ld returned 1 exit status
- 全面详细的jQuery常见开发技巧手册
- 用JSP输出九九乘法表
- js判断对象是否存在的三种方法
- EL、JSP
- (4)javascript模块化之AMD规范
- 【ife】任务二十一:基础JavaScript练习(四)