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 ...
随机推荐
- Linux下修改hostname
我维护两三个机房的数十台机器,开发用机器,运营用机器,自己工作机器也是ubuntu,有时开很多ssh,干的还是同样的事情,很容易搞混.所以需要一目了然的知道某台机器的情况,避免犯晕.这就需要修改主机名 ...
- C# List 中 Find 方法
实例化一个集合 List<User> userCollection = new List<User>(); userCollection.Add(new User(1, &qu ...
- Uva10474 - Where is the Marble?
两种解法: 1.计数排序 //计数排序 #include<cstdio> #include<iostream> #include<vector> #includ ...
- PL/pgSQL的anyelement例子
http://www.postgresonline.com/journal/archives/239-The-wonders-of-Any-Element.html 定义函数 pgsql=# CREA ...
- ARM&Linux 下驱动开发第三节
后台驱动代码如下:比较昨天的,添加了读写指针位置移动操作 #include<linux/init.h> #include<linux/module.h> #include< ...
- [转]iOS应用性能调优的25个建议和技巧
写在前面 本文来自iOS Tutorial Team 的 Marcelo Fabri,他是Movile的一名 iOS 程序员.这是他的个人网站:http://www.marcelofabri.com/ ...
- BZOJ 1045: [HAOI2008] 糖果传递 数学
1045: [HAOI2008] 糖果传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1045 Description 有n个小朋友坐 ...
- 移动端折腾国外分享(facebook、twitter、linkedin)
一.前言 国内做HTML5页面,关注最多就是微信分享了,之前也写过关于微信分享的文章,可以点击查看:分享相关文章 再者,就是国内的其它分享,比如常用的新浪微博.腾讯微博.QQ空间等等,最方便的就是直接 ...
- 【JavaScript】Understanding callback functions in Javascript
Callback functions are extremely important in Javascript. They’re pretty much everywhere. Originally ...
- [Angular2 Form] Display Validation and Error Messaging in Angular 2
Angular 2’s ngModel provides error objects for each of the built-in input validators. You can access ...