A Bug's Life --- 种类并查集
来源:互联网 发布:canbot 软件 编辑:程序博客网 时间:2024/06/05 20:05
Time Limit: 10000MS
Memory Limit: 65536KTotal Submissions: 27710
Accepted: 9010
Memory Limit: 65536KTotal Submissions: 27710
Accepted: 9010
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.
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
23 31 22 31 34 21 23 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
解题分析:
此题的重点是在如何推出相关的关系公式,找到其种类的关系。
可以看出只要两个虫子序号之差是奇数说明就是同性。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAXN 2005
bool flag;
int fx, fy, n;
int pre[MAXN], relate[MAXN];
int Find(int x)
{
if(pre[x] == -1)
return x;
else
{
int tmp = pre[x];
pre[x] = Find(pre[x]);
relate[x] = (relate[x]+relate[tmp])%2;
return pre[x];
}
}
int main()
{
int T, m, a, b;
scanf("%d", &T);
for(int k = 1; k <= T; ++k)
{
flag = true;
scanf("%d%d", &n, &m);
{
memset(pre, -1, (n+1)*sizeof(int));
memset(relate, 0, (n+1)*sizeof(int));
while(m--)
{
scanf("%d%d", &a, &b);
if(!flag)
continue;
fx = Find(a);
fy = Find(b);
if(fx != fy)
{
pre[fx] = fy;
relate[fx] = (relate[a] + relate[b] + 1)%2;
}
else
{
if(relate[a] == relate[b])
flag = false;
}
}
printf("Scenario #%d:\n", k);
if(flag)
printf("No suspicious bugs found!\n\n");
else
printf("Suspicious bugs found!\n\n");
}
}
return 0;
}
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAXN 2005
bool flag;
int fx, fy, n;
int pre[MAXN], relate[MAXN];
int Find(int x)
{
if(pre[x] == -1)
return x;
else
{
int tmp = pre[x];
pre[x] = Find(pre[x]);
relate[x] = (relate[x]+relate[tmp])%2;
return pre[x];
}
}
int main()
{
int T, m, a, b;
scanf("%d", &T);
for(int k = 1; k <= T; ++k)
{
flag = true;
scanf("%d%d", &n, &m);
{
memset(pre, -1, (n+1)*sizeof(int));
memset(relate, 0, (n+1)*sizeof(int));
while(m--)
{
scanf("%d%d", &a, &b);
if(!flag)
continue;
fx = Find(a);
fy = Find(b);
if(fx != fy)
{
pre[fx] = fy;
relate[fx] = (relate[a] + relate[b] + 1)%2;
}
else
{
if(relate[a] == relate[b])
flag = false;
}
}
printf("Scenario #%d:\n", k);
if(flag)
printf("No suspicious bugs found!\n\n");
else
printf("Suspicious bugs found!\n\n");
}
}
return 0;
}
0 0
- poj 2492 A Bug's Life(种类并查集)
- A Bug's Life(hdu1829种类并查集)
- 【种类并查集】-POJ-2492-A bug's life
- HDU 1829 A Bug's Life(种类并查集)
- A Bug's Life 种类并查集
- poj2492 A Bug's Life 种类并查集
- A Bug's Life --- 种类并查集
- hdoj-1289-A Bug's Life【种类并查集】
- HDU 1829 A Bug's Life【种类并查集】
- HDU1829:A Bug's Life(种类并查集)
- HDU1829 A Bug's Life(种类并查集)
- 【种类并查集】 hdu1829 A bug's Life
- A Bug's Life(种类并查集)
- POJ2492 -A Bug's Life(种类并查集)
- A Bug's Life 并查集
- 并查集 A Bug's Life
- A Bug's Life 并查集
- HDU 1829 A Bug's Life(基础种类并查集)
- 南京邮电大学OJ——1061简单选择排序
- 斐波那契的矩阵快速幂
- Android中SQLiteOpenHelper类的onUPgrade()方法的作用
- 【cocos2d-x-3.1.1系列1】VS2012+win7(64)+cocos2d-x-3.1.1的新建工程、打开新建的工程
- 《C++ Primer Plus》学习笔记6
- A Bug's Life --- 种类并查集
- sql语句练习1
- Linux操作系统启动过程详解
- Largest Rectangle in a Histogram - HDU 1506 dp
- 设计模式-状态模式
- 2014 BUPT 新生排位赛01
- 使用Fragment兼容手机平板-之设置主界面的模仿简单实现
- @synthesize window=_window; 的理解
- Android使用 LruCache 缓存图片