hdu 1829 A Bug's Life
2015-07-23 17:39
344 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1829
A Bug's Life
Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status
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.
搜别人的博客,别人都说这是道水题,可是我还是不会,好伤心伤心
A Bug's Life
Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status
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.
准确点说应该是读入的这对数x,y,到树根的距离的奇偶性是不可以相同的,因为如果他们到根的奇偶性相同,就说明他们是同性
搜别人的博客,别人都说这是道水题,可是我还是不会,好伤心伤心
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include <map> using namespace std; #define N 125000 #define INF 0x3f3f3f3f #define PI acos (-1.0) #define EPS 1e-8 int n, m, f , r ; int Find (int x) { int t = f[x]; if (x != f[x]) { f[x] = Find (f[x]); r[x] = (r[x] + r[t]) % 2; } return f[x]; } int main () { int t, k = 1; cin >> t; while (t--) { cin >> n >> m; for (int i=1; i<=n; i++) { f[i] = i; r[i] = 0; } int a, b, flag = 1; for (int i=0; i<m; i++) { cin >> a >> b; int x = Find (a), y = Find (b); if (x == y) { if (r[a] == r[b])//性别相同 flag = 0; } else { f[x] = y;//性别不同,就把两个归为一个根节点 r[x] = (r[a] + r[b] + 1) % 2; } } cout << "Scenario #" << k++ << ':' << endl; if (!flag) cout << "Suspicious bugs found!" << endl; else cout << "No suspicious bugs found!" << endl; cout << endl; } return 0; }
相关文章推荐
- 关于在jquery动态修改css,html中,mouseenter,mouseleave,click等方法失效的处理
- 函数式、响应式编程 - ReactiveCocoa 快速入门
- 参数传递的四种形式----- URL,超链接,js,form表单
- java 和 javascript中空值和字符串的比较
- nodejs Buffer.fill和JSON.stringify在操作系统之间的不同反应
- js中判断是否为数字
- extjs_09_定义自己的页面组件
- ReactiveCocoa P1
- zf-关于荆州首页鼠标移动到导航栏上去触发的js 显示 问题解决办法
- 一个人的旅行(floyd+dijskra+SPFA+Bellman)
- javascript如何定义全局变量
- background-origin
- javascript相对定位常用的函数
- js的Prototype属性 解释及常用方法
- fastjson用法3
- CSS基础
- 前端自动化神器gulp使用记录
- Broswer内核简单小结
- HTML基础总结
- CSS中固定宽度布局的详细教程