HDU 1829 - A Bug's Life
2016-01-27 19:47
543 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
[align=left]Sample Output[/align]
Scenario #1:
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!
Hint
Huge input,scanf is recommended.
大致题意:
检查一堆数据中是否有同性恋,找出主要矛盾是如果1交配2,2交配3,而1又交配3,则矛盾。找到一组就行。
解题思路:
用并查集做元素关系判断,主要建立余数体系;
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
[align=left]Sample Output[/align]
Scenario #1:
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!
Hint
Huge input,scanf is recommended.
大致题意:
检查一堆数据中是否有同性恋,找出主要矛盾是如果1交配2,2交配3,而1又交配3,则矛盾。找到一组就行。
解题思路:
用并查集做元素关系判断,主要建立余数体系;
#include <cstdio> using namespace std; int f[2000+5],num[2000+5],t,n,m,a,b,fa,fb; int sf(int x){ if(x==f[x]) return x; else { int fx=sf(f[x]); num[x]=(num[x]+num[f[x]])%2; return f[x]=fx; } } int main(){ scanf("%d",&t); for(int k=1;k<=t;k++){ printf("Scenario #%d:\n",k); scanf("%d%d",&n,&m); bool flag=1; for(int i=0;i<=n;i++) f[i]=i,num[i]=0; for(int i=1;i<=m;i++){ scanf("%d%d",&a,&b); if(flag){ fa=sf(a); fb=sf(b); if(fa==fb){ if((num[a]-num[b]+2)%2!=1) flag=0;}//避免负数 else f[fb]=fa,num[fb]=(num[a]-num[b]+1+2)%2; } } if(flag) puts("No suspicious bugs found!\n"); else puts("Suspicious bugs found!\n"); } return 0; }
相关文章推荐
- 选择表单html
- 一篇给迷茫者看的JavaScript 引擎指南
- JS判断字符串变量是否含有某个字串的方法
- 拒offer
- spark的slave上没有datanode 的解决办法
- Nth to Last Node in List
- 使用jquery获取父元素或父节点的方法
- 第 10 章 css 样式设置小技巧
- js模板引擎
- Mina 2.0.9-详解3-Reactor
- js获取客户时间倒计时,js获取服务器端时间倒计时
- node-zk-browser 安装配置
- 关于JS变量提升的一些坑
- Gson和Fastjson的使用
- The Java™ Tutorials — Generics :Type Inference 类型推断
- CSS---堆叠顺序---position---z-index
- 前端--关于javascript对象
- Feescale K60开发笔记3: Tftpd32的使用
- AngularJS系列:10、作用域
- angularjs ajax传参