POJ 2492 A Bug's Life (并查集)
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 30130 | Accepted: 9869 |
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! 判断两只虫子是否同性。
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
int FATHER[SIZE],MARK[SIZE],RANK[SIZE]; void ini(int);
int get_father(int);
void unite(int,int);
bool same(int,int); int main(void)
{
int t,n,m,x,y,count = ;
bool flag; scanf("%d",&t);
while(t --)
{
count ++;
scanf("%d%d",&n,&m);
ini(n);
flag = false;
while(m --)
{
scanf("%d%d",&x,&y);
if(flag)
continue;
if(same(x,y))
flag = true;
if(!MARK[x] && !MARK[y])
{
MARK[x] = y;
MARK[y] = x;
}
else if(!MARK[x])
{
MARK[x] = y;
unite(x,MARK[y]);
}
else if(!MARK[y])
{
MARK[y] = x;
unite(y,MARK[x]);
}
else
{
unite(x,MARK[y]);
unite(y,MARK[x]);
}
}
printf("Scenario #%d:\n",count);
if(flag)
puts("Suspicious bugs found!");
else
puts("No suspicious bugs found!");
puts("");
} return ;
} void ini(int n)
{
for(int i = ;i <= n;i ++)
{
MARK[i] = RANK[i] = ;
FATHER[i] = i;
}
} int get_father(int n)
{
if(n == FATHER[n])
return n;
return FATHER[n] = get_father(FATHER[n]);
} void unite(int x,int y)
{
x = get_father(x);
y = get_father(y); if(x == y)
return ;
if(RANK[x] < RANK[y])
FATHER[x] = y;
else
{
FATHER[y] = x;
if(RANK[x] == RANK[y])
RANK[x] ++;
}
} bool same(int x,int y)
{
return get_father(x) == get_father(y);
}
POJ 2492 A Bug's Life (并查集)的更多相关文章
- nyoj 209 + poj 2492 A Bug's Life (并查集)
A Bug's Life 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Background Professor Hopper is researching th ...
- POJ 2492 A Bug's Life 并查集的应用
题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...
- A Bug's Life POJ - 2492 (种类或带权并查集)
这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- 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 ...
- hdu 1829 &poj 2492 A Bug's Life(推断二分图、带权并查集)
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- poj 2492 A Bug's Life 二分图染色 || 种类并查集
题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...
随机推荐
- 可执行jar包的maven配置
待整理 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.or ...
- JavaScript事件处理的三种方式(转)
一.什么是JavaScript事件? 事件(Event)是JavaScript应用跳动的心脏,也是把所有东西粘在一起的胶水,当我们与浏览器中Web页面进行某些类型的交互时,事件就发生了. 事件可能是用 ...
- asp获取勾选checkbox的值
Dim str_select str_select = CStr(request.Form("c_name")) c_name是checkbox的name
- js 函数的传值问题
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- JavaScript 各种遍历方式详解,有你不知道的黑科技
http://segmentfault.com/a/1190000003968126 为了方便例子讲解,现有数组和json对象如下 var demoArr = ['Javascript', 'Gulp ...
- jsp中使用动态数据进行mySQL数据库的两种操作方法
使用动态数据进行数据库内容的增删改查操作有两种方法: 在此定义数据库连接为conn 假设有表单进行数据输入并提交到处理页面一种是使用预编译格式: 其格式如下: String name = reques ...
- css 文字超出变 ... 点点点
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
- 【转载】Fragment 全解析(1):那些年踩过的坑
http://www.jianshu.com/p/d9143a92ad94 Fragment系列文章:1.Fragment全解析系列(一):那些年踩过的坑2.Fragment全解析系列(二):正确的使 ...
- CSS去除链接虚线(兼容IE6、IE7)
在css里加入以下代码: a{ hide-focus: expression( this.hideFocus=true ); outline: none;}
- asp.net 后台获取flv视频地址进行播放【转】
源码下载:http://download.csdn.net/detail/njxiaogui/7609687 前台:.aspx <table> <tr> <td>& ...