【POJ】2492 A bug's life ——种类并查集
A Bug's Life
| Time Limit: 10000MS | Memory Limit: 65536K | |
| Total Submissions: 28211 | Accepted: 9177 |
Description
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
Output
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
#include <cstdio>
#include <cstring> const int LEN = ; int uset[LEN];
int opp[LEN]; //代表每个虫子的配对异性
int rank[LEN];
int n; void makeset() //初始化并查集
{
for(int i = ; i <= n; i++)
uset[i] = i;
memset(opp, , sizeof(opp));
memset(rank, , sizeof(rank));
} int findset(int x)
{
if (x == uset[x])
return x;
else
uset[x] = findset(uset[x]);
return uset[x];
} void unionset(int x, int y)
{
if ((x = findset(x)) == (y = findset(y)))
return;
if (rank[x] > rank[y]) //按秩合并
uset[y] = x;
else{
uset[x] = y;
if (rank[x] == rank[y])
++rank[y];
}
} int main()
{
int T;
scanf("%d", &T);
for(int cnt = ; cnt <= T; cnt++){
int m;
scanf("%d %d", &n, &m);
makeset();
int f = ;
for(int i = ; i < m; i++){
int x, y;
scanf("%d %d", &x, &y);
if (f)
continue;
if (findset(x) == findset(y)){ //如果这两个虫子在同一个集合里找到 就是同性恋
f = ;
continue;
}
if (opp[x] == && opp[y] == ){ //代表两个虫子都没被分类过,储存下每个虫子的异性
opp[x] = y;
opp[y] = x;
}
else if (opp[x] == ){ //如果x没被分类过,将它的异性记录为y,并将它加入y的异性所在的集合
opp[x] = y;
unionset(x, opp[y]);
}
else if (opp[y] == ){ //同上
opp[y] = x;
unionset(y, opp[x]);
}
else{ //否者合并x也opp[y]所在的集合以及y与opp[x]所在的集合
unionset(x, opp[y]); unionset(y, opp[x]);
}
}
printf("Scenario #%d:\n", cnt);
if (f)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
printf("\n");
}
return ;
}
【POJ】2492 A bug's life ——种类并查集的更多相关文章
- POJ 2492 A Bug's Life (并查集)
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- POJ 2492 A Bug's Life【并查集高级应用+类似食物链】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- POJ 2492 A Bug's Life(并查集)
http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...
- POJ2492 A Bug's Life —— 种类并查集
题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Su ...
- POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...
- HDU 1829 A Bug's Life(种类并查集)
思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...
- hdu1829A Bug's Life(种类并查集)
传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- hdoj 1829 A bug's life 种类并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...
随机推荐
- [Backbone.js]如何用backbone写一个仿网页版微信的webapp?
var Chat = Backbone.Model.extend({ idAttribute:'id', initialize:function(options){ var users = this. ...
- jQuery validate (转载)
转自:http://blog.sina.com.cn/s/blog_608475eb0100h3h1.html jQuery校验 官网地址:http://bassistance.de/jquery-p ...
- js数值转换
先来几个题吧: var num1 = Number("123blue");var num2 = Number("");var num3 = Number([]) ...
- QT 线程池 + TCP 小试(一)线程池的简单实现
*免分资源链接点击打开链接http://download.csdn.net/detail/goldenhawking/4492378 很久以前做过ACE + MFC/QT 的中轻量级线程池应用,大概就 ...
- Azure 为 SAP 提供的增强监控解决方案
正如大家在之前公告中所看到的,多个SAP应用程序已获得认证可在 Azure虚拟机中运行,未来还会有更多 SAP应用程序获得认证.对于希望在 Azure中运行 SAP应用程序的客户来说,这是个激动人心的 ...
- Ubuntu 12.04下安装ibus中文输入法
这是最完整的安装方法: ibu是一个框架,可以支持多种输入法,像是pinyin,五笔等. 1,安装ibus框架 终端输入以下命令: sudo apt-get install ibus ibus-clu ...
- Windows下使用XManager访问Ubuntu 11.04的设置方法
1.Download the attached file and upload it to the server. And, enter the following command to update ...
- Linux学习十八之、善用判断式
原文地址:http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_3.php 善用判断式 在第十一章中,我们提到过 $? 这个变量所 ...
- 一,入门基础—— 2. 第一个project项目
1. 欢迎界面的右边是一个项目列表,显示全部近期打开的项目,双击⭕️打开之前创建的项目. 2.右击⭕️处,选择"Add Files to DemoApp..."加入一张图片. 3. ...
- Java NIO——2 缓冲区
一.缓冲区基础 1.缓冲区并不是多线程安全的. 2.属性(容量.上界.位置.标记) capacity limit 第一个不能被读或写的元素 position 下一个要被读或写的元素索引 mark ...