A Bug's Life

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述
Background 
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.
 
输入
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 10000) and the number of interactions (up to 1000000) separated by a single space. 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.
输出
The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
样例输入
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
样例输出
Scenario #1:
Suspicious bugs found! Scenario #2:
No suspicious bugs found!
 /**
题目大意:
判断输入的两个bug(臭虫)是否是同性恋,教授认为他判断的臭虫都是异性的,你的任务就是看教授
的判断是否真确。
①、如果有两个臭虫是同性的,那么输出 Suspicious bugs found! (找到可疑(同性)臭虫)
②、如果 *bug(所有的bug) 都不是同性的,最后输出 no Suspicious bugs found! (没找到可疑(同性)臭虫) 解题算法:并查集 idea:
1、将同性的所有臭虫放在一起。
1.1、如果输入的数据在同一个集合中说明这对臭虫是可疑的
1.2、如果输入的数据不在同一个集合说明它们到现在都还是异性关系
1.2.1、同时,我们假设数据a和数据a+n是异性关系
1.2.1、那么如果a 与 b也是异性关系,就有a+n 与 b是同性(放在同一个集合中) PS:其实,这是一个模板题,只要是这种关系:
<数据只有两种情况、后一个数据的状态会被前面的数据影响、判断新插入的数据是否合法>
**/

核心模板:

 void my_init(int n) // 数据的初始化
{
for(int i = ; i <= n; ++ i)
pre[i] = i;
}
int my_find(int x) // 找到它的根节点
{
int n = x;
while (n != pre[n])
n = pre[n];
int i = x, j;
while (pre[i] != n) //路径压缩
{
j = pre[i];
pre[i] = n;
i = j
}
return n;
}
void my_join(int a, int b) // 不是同一个根节点的数据加入
{
int n1 = my_find(a), n2 = my_find(b);
if (n1 != n2)
pre[n1] = n2;
}
bool my_judge(int a, int b) // 判断是否在同一个集合中
{
int n1 = my_find(a), n2 = my_find(b);
if (n1 != n2) return true;
return false;
}

C/C++代码实现(AC):

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; int T, n, m, pre[], flag, cnt; void my_init()
{
int nn = n + n;
for (int i = ; i <= nn; ++ i)
pre[i] = i;
return ;
} int my_find(int x)
{
int n = x;
while (n != pre[n])
n = pre[n];
int i = x, j;
while (pre[i] != n)
{
j = pre[i];
pre[i] = n;
i = j;
}
return n;
} bool judge(int a, int b)
{
int n1 = my_find(a), n2 = my_find(b);
if (n1 != n2) return true;
return false;
} void my_join(int a, int b)
{
int n1 = my_find(a), n2 = my_find(b);
if (n1 != n2) pre[n1] = n2;
return ;
} int main()
{
cnt = ;
scanf("%d", &T);
for(int i = ; i < T; ++ i)
{
flag = ;
scanf("%d%d", &n, &m);
my_init();
while (m --)
{
int a, b;
scanf("%d%d", &a, &b);
if (flag) continue; if (judge(a, b) || judge(a + n, b + n))
{
my_join(a, b + n);
my_join(a + n, b);
}
else
flag = ;
} if (i != ) puts("");
if (flag)
printf("Scenario #%d:\nSuspicious bugs found!\n", cnt ++);
else
printf("Scenario #%d:\nNo suspicious bugs found!\n", cnt ++); }
return ;
}

nyoj 209 + poj 2492 A Bug's Life (并查集)的更多相关文章

  1. POJ 2492 A Bug's Life (并查集)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 30130   Accepted: 9869 De ...

  2. POJ 2492 A Bug's Life 并查集的应用

    题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...

  3. A Bug's Life POJ - 2492 (种类或带权并查集)

    这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...

  4. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

  5. 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条臭 ...

  6. (简单) POJ 2492 A Bug's Life,二分染色。

    Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...

  7. POJ 2492 A Bug's Life (并查集)

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  8. POJ 2492 A Bug's Life【并查集高级应用+类似食物链】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  9. hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. php反序列化漏洞复现过程

    PHP反序列化漏洞复现 测试代码 我们运行以上代码文件,来证明函数被调用: 应为没有创建对象,所以构造函数__construct()不会被调用,但是__wakeup()跟__destruct()函数都 ...

  2. 《如何学习基于ARM嵌入式系统》笔记整理

    author:Peong time:20190603 如何学习基于ARM嵌入式系统 一.嵌入式系统的概念 从硬件上讲,将外围器件,与CPU集成在一起. 从操作系统上讲,定制符合要求的系统内核 从应用上 ...

  3. Spring Data - Spring Data JPA 提供的各种Repository接口

    Spring Data Jpa 最近博主越来越懒了,深知这样不行.还是决定努力奋斗,如此一来,就有了一下一波复习 演示代码都基于Spring Boot + Spring Data JPA 传送门: 博 ...

  4. django-模板之with标签(十二)

    就是给传过来的值取个别名.

  5. python【控制台】小游戏--贪吃蛇

    传统贪吃蛇相信大家都玩过,也是一款很老很经典的游戏,今天我们用python控制台实现 项目有很多bug没有解决,因为本人一时兴起写的一个小游戏,所以只是实现可玩部分功能,并没有花较多的时间和精力去维护 ...

  6. TensorFlow初学教程(完整版)

    1:你想要学习TensorFlow,首先你得安装Tensorflow,在你学习的时候你最好懂以下的知识:    a:怎么用python编程:     b:了解一些关于数组的知识:     c:最理想的 ...

  7. ES6对象简洁语法

    对象(object)是 JavaScript 最重要的数据结构.ES6 对它进行了重大升级,本章介绍数据结构本身的改变及语法应用细节. 1.属性的简洁表示法 ◆ ES6 允许直接写入变量和函数,作为对 ...

  8. Android H5混合开发(3):原生Android项目里嵌入Cordova

    前言 如果安卓项目已经存在了,那么如何使用Cordova做混合开发? 方案1(适用于插件会持续增加或变化的项目): 新建Cordova项目并添加Android平台,把我们的安卓项目导入Android平 ...

  9. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

  10. Java学习总结之方法重载和方法重写

    在学习方法的阶段我学习了方法重载(Overload),而在学习面向对象三大特性之继承的时候我又学习了方法重写(Override).   概念: 方法重载:在同一个类中,允许存在一个以上的同名方法,只要 ...