【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 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...
随机推荐
- PHP获取真实的网络IP
function get_client_ip() { $ip = $_SERVER['REMOTE_ADDR']; if (isset($_SERVER['HTTP_CLIENT_IP']) & ...
- HTML5新属性-----拖放
最早引入JS拖放功能的是IE4,当时,网页中只有两种对象可以拖放:图像和某些文本.拖动图像时,把鼠标放在图像上,按住鼠标不放就可以拖动它.拖动文本时,需要选中文本,然后可以像拖动图像一样拖动被选中的文 ...
- python中实现多线程的几种方式
python实现多线程的方式大概有 1.threading 2._thread #!/usr/bin/python #!coding:utf-8 import threading def action ...
- linux 进程(一)---基本概念
一.进程的定义 进程是操作系统的概念,每当我们执行一个程序时,对于操作系统来讲就创建了一个进程,在这个过程中,伴随着资源的分配和释放.可以认为进程是一个程序的一次执行过程. 二.进 ...
- Unity2D屏幕适配方案
看了cnblogs里的一篇文章,终于理解了Unity2D的摄像机系统:http://www.cnblogs.com/flyFreeZn/p/4073655.html 我根据他的方案,改写了两种适配方案 ...
- 克拉夫斯曼高端定制 刘霞---【YBC中国国际青年创业计划】
克拉夫斯曼高端定制 刘霞---[YBC中国国际青年创业计划] 克拉夫斯曼高端定制 刘霞
- C#中关于DateTime的最大值和最小值
System.DateTime的最小可能值:DateTime.MinValue.ToString()=0001-1-1 0:00:00 我们实际用的时候会指定一个默认值DateTime.Parse(& ...
- LeetCode-001 Two Sum
[题目] Given an array of integers, find two numbers such that they add up to a specific target number. ...
- ACM/ICPC2014鞍山现场赛E hdu5074Hatsune Miku
题目链接:pid=5074">http://acm.hdu.edu.cn/showproblem.php?pid=5074 题意: 给定一个m*m的矩阵mp.然后给定一个长度为n的序列 ...
- CRM odata方法如何使用$top
odata方法 $top $top1 取1个 ¥top100取100个,放在$select前,中间用&符号隔开. 例如: var activeserviceReq = "/xrmse ...