poj2492 A Bug's Life【并查集】
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
In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
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
假设虫子只和异性交流 现在告诉你交流的虫子号码 问这个假设正确不正确
现在发现并查集就是找节点和父节点的关系 rank数组就是x对fx的关系 有时候不考虑方向比如这一题
像这种题目更新的时候 fx的rank应该是 x对fx的关系加fy对y的关系加x对y的关系
输出之间要空行!
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 2005;
int t, n, m;
int ran[maxn], parent[maxn];
void init(int n)
{
for(int i = 0; i < n; i++){
ran[i] = 0;
parent[i] = i;
}
}
int fin(int x)
{
if(x == parent[x]) return x;
int t = parent[x];
parent[x] = fin(parent[x]);
ran[x] = (ran[x] + ran[t]) % 2;
return parent[x];
}
void mer(int x, int y)
{
int tx = fin(x);
int ty = fin(y);
if(tx != ty){
parent[tx] = ty;
ran[tx] = (ran[x] + 1 + ran[y]) % 2;
}
}
int main()
{
scanf("%d", &t);
for(int i = 1; i <= t; i++){
scanf("%d%d", &n, &m);
init(n);
bool flag = true;
for(int j = 0; j < m; j++){
int a, b;
scanf("%d%d", &a, &b);
if(fin(a) == fin(b)){
if(ran[a] == ran[b])
{
flag = false;
}
}
else{
mer(a, b);
}
}
if(!flag){
printf("Scenario #%d:\nSuspicious bugs found!\n\n", i);
}
else{
printf("Scenario #%d:\nNo suspicious bugs found!\n\n", i);
}
}
return 0;
}
poj2492 A Bug's Life【并查集】的更多相关文章
- [poj2492]A Bug's Life(并查集+补集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 34678 Accepted: 11339 D ...
- hdu 1829 A Bug's Life(并查集)
A Bu ...
- J - A Bug's Life 并查集
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- nyoj 209 + poj 2492 A Bug's Life (并查集)
A Bug's Life 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Background Professor Hopper is researching th ...
- poj 2492A Bug's Life(并查集)
/* 目大意:输入一个数t,表示测试组数.然后每组第一行两个数字n,m,n表示有n只昆虫,编号从1—n,m表示下面要输入m行交配情况,每行两个整数,表示这两个编号的昆虫为异性,要交配. 要求统计交配过 ...
- 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 并查集的应用
题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...
- poj2492_A Bug's Life_并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 34947 Accepted: 11459 D ...
- POJ 2492 A Bug's Life (并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 30130 Accepted: 9869 De ...
- A Bug's Life____并查集
English preparation: falsify 伪造:篡改:歪曲:证明...虚假 the sexual behavior of a rare species of bugs. 一种稀 ...
随机推荐
- 计算 md5
代码从polarssl中扒来的,略作改动,md5.h & md5.cpp 如下 #ifndef POLARSSL_MD5_H #define POLARSSL_MD5_H #include & ...
- iOS protocbuf安装使用
protobuf文件地址:https://github.com/google/protobuf 1.问题/usr/local.bak/lib /usr/local.bak/man /usr/local ...
- 配置Django框架为生产环境的注意事项(DEBUG=False)
问题描述: Django1.10版本中框架中settings.py配置文件 配置文件settings.py配置了下面两项: DEBUG= False ALLOWED_HOSTS = ['*'] #这样 ...
- 【LeetCode OJ】Median of Two Sorted Arrays
题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 ...
- input回车问题
今天有一个问题,就是input对象没有加任何事件自己回车导致跳到了404页面.处理的时候,并发现没找到回车事件的控制. 那么只有一种情况,就是自带的回车控制. 百度了一下,如下面博文里面的写法.我这边 ...
- linux计划任务之crontab
语法: crontab [ -u user ] file crontab [ -u user ] [ -i ] { -e | -l | -r } 说明: crontab命令 ...
- css笔记 - 张鑫旭css课程笔记之 padding 篇
[padding地址](https://www.imooc.com/learn/710) 一.padding与容器尺寸之间的关系 padding会影响元素的尺寸(通常情况下是通过增加/挤压内容区域) ...
- C# EF中调用 存储过程并调回参数
TourEntities db = new TourEntities(); List<v_product> v = new List<v_product>(); SqlPara ...
- HTML5 css3 阴影效果
阴影效果曾让 Web 设计师既爱又恨,现在,有了 CSS3,你不再需要 Photoshop,已经有网站在使用这个功能了,如 24 Ways website. -webkit-box-shadow: 1 ...
- ARC下带CF前缀的类型与OC类型转换
在对钥匙串操作时这个函数 OSStatus SecItemCopyMatching(CFDictionaryRef query, CFTypeRef * __nullable CF_RETURNS_R ...