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 ...
随机推荐
- 浅析 JavaScript 中的闭包(Closures)
a { text-decoration: none; color: #4094c7 } h4,h5 { margin: 0; font-weight: 700; color: inherit; lin ...
- java(2014版)连接数据库的工具类
package util; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; ...
- Custom ReadOnlyProperty【PluraSight】
Limited functionality: Not settable No data binding No validation No animation No Inheritance When t ...
- HttpContext及HttpContext.current
慎用System.Web.HttpContext.Current http://www.cnblogs.com/david1989/p/3879201.html 线程编程中用到HttpContext. ...
- ActionBar PopuMenu
PopupMenu popupmenu可以非常方便得实现在指定view下弹出一个菜单,实现类似ActionBar中的效果. public void showPopupMenu(View view){ ...
- JS函数的定义与调用方法
JS函数调用的四种方法:方法调用模式,函数调用模式,构造器调用模式,apply,call调用模式 1.方法调用模式:先定义一个对象,然后在对象的属性中定义方法,通过myobject.property来 ...
- Java数据结构之线性表(2)
从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...
- 11个新鲜出炉的jQuery图像滑块插件
如今图像滑块已成为一种流行的Web设计元素,通过滑块,你可以在有限的页面空间中展示更多的内容,带给用户更佳的浏览体验.本文将为你带来一些非常实用的jQuery图像滑块插件. 1. Basic Sli ...
- Codeforces Round #311 (Div. 2) C. Arthur and Table Multiset
C. Arthur and Table Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...
- Codeforces Round #307 (Div. 2) A. GukiZ and Contest 水题
A. GukiZ and Contest Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...