POJ 2492 A Bug's Life
2016-07-09 22:45
369 查看
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
Sample Output
Hint
Huge input,scanf is recommended.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
并查集。然而我超时了······
(要输出两个换行符······)
这个是超时的代码······#include<cstdio>
#include<cstring>
int t,n,m,fa[2006],k,x,y;
bool b[2006];
int findd(int u)
{
if(fa[u]==u) return u;
return findd(fa[u]);
}
int qiou(int u,int kk)
{
if(fa[u]==u) return kk%2;
return qiou(fa[u],(++kk)%2);
}
int main()
{
scanf("%d",&t);
for(int u=1;u<=t;u++)
{
scanf("%d%d",&n,&m); //n虫子,m组关系
k=0;
memset(b,0,sizeof(b));
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(!b[x] && !b[y]) b[x]=b[y]=1,fa[y]=x;
if(b[x] && !b[y]) fa[y]=x,b[y]=1;
if(b[y] && !b[x]) fa[x]=y,b[x]=1;
if(b[x] && b[y])
{
if(findd(x)==findd(y)) //在同一棵树中
if(qiou(x,1)==qiou(y,1))
{
k=1;break;
}
if(findd(x)!=findd(y)) fa[x]=y; //不在同一棵树中
}
}
printf("Scenario #%d:\n",u);
if(k) printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
}
return 0;
}
然而它改起来太麻烦了······ 于是我又换了一种方法······ 参考图神的代码······
这种方法感觉好神奇啊······
#include<cstdio>
#include<cstring>
int t,n,m,fa[4006],k,x,y,d[4006];
inline int pan(int u)
{
if(fa[u]==u)
{
return u;
}
return pan(fa[u]);
}
int main()
{
scanf("%d",&t);
for(int u=1;u<=t;u++)
{
scanf("%d%d",&n,&m); //n虫子,m组关系
k=0;
for(int i=1;i<=2*n;i++) fa[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(pan(x)==pan(y)) k=1;
else
{
fa[pan(x+n)]=pan(y);
fa[pan(x)]=pan(y+n);
}
}
printf("Scenario #%d:\n",u);
if(k) printf("Suspicious bugs found!\n\n");
else printf("No suspicious bugs found!\n\n");
}
return 0;
}
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.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
并查集。然而我超时了······
(要输出两个换行符······)
这个是超时的代码······#include<cstdio>
#include<cstring>
int t,n,m,fa[2006],k,x,y;
bool b[2006];
int findd(int u)
{
if(fa[u]==u) return u;
return findd(fa[u]);
}
int qiou(int u,int kk)
{
if(fa[u]==u) return kk%2;
return qiou(fa[u],(++kk)%2);
}
int main()
{
scanf("%d",&t);
for(int u=1;u<=t;u++)
{
scanf("%d%d",&n,&m); //n虫子,m组关系
k=0;
memset(b,0,sizeof(b));
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(!b[x] && !b[y]) b[x]=b[y]=1,fa[y]=x;
if(b[x] && !b[y]) fa[y]=x,b[y]=1;
if(b[y] && !b[x]) fa[x]=y,b[x]=1;
if(b[x] && b[y])
{
if(findd(x)==findd(y)) //在同一棵树中
if(qiou(x,1)==qiou(y,1))
{
k=1;break;
}
if(findd(x)!=findd(y)) fa[x]=y; //不在同一棵树中
}
}
printf("Scenario #%d:\n",u);
if(k) printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
}
return 0;
}
然而它改起来太麻烦了······ 于是我又换了一种方法······ 参考图神的代码······
这种方法感觉好神奇啊······
#include<cstdio>
#include<cstring>
int t,n,m,fa[4006],k,x,y,d[4006];
inline int pan(int u)
{
if(fa[u]==u)
{
return u;
}
return pan(fa[u]);
}
int main()
{
scanf("%d",&t);
for(int u=1;u<=t;u++)
{
scanf("%d%d",&n,&m); //n虫子,m组关系
k=0;
for(int i=1;i<=2*n;i++) fa[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(pan(x)==pan(y)) k=1;
else
{
fa[pan(x+n)]=pan(y);
fa[pan(x)]=pan(y+n);
}
}
printf("Scenario #%d:\n",u);
if(k) printf("Suspicious bugs found!\n\n");
else printf("No suspicious bugs found!\n\n");
}
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C++的template模板中class与typename关键字的区别分析
- C与C++之间相互调用实例方法讲解