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. Ubuntu 搜狗输入法输入异常

    电脑放置一段时间,不使用.过了一会,再使用 sogou 输入法的时候,发现,输入法不起作用了. 切花到用户目录 ~/.config 里面 rm -rf Sogou* 删除搜狗的配置文件,退出当前账户, ...

  2. Javascript之傻傻理不清的原型链、prototype、__proto__

    新人学习Javascript,其中的原型链一直是云里雾里,不得要领,查了很多相关资料,觉得这遍讲得最为清晰易懂,特转载分享,共同学习. 1. JavaScript内置对象 所谓的内置对象 指的是:Ja ...

  3. spark运行java-jar:Exception in thread "main" java.io.IOException: No FileSystem for scheme: hdfs

    今天碰到的一个 spark问题,困扰好久才解决 首先我的spark集群部署使用的部署包是官方提供的 spark-1.0.2-bin-hadoop2.tgz 部署在hadoop集群上. 在运行java ...

  4. 【python数据分析实战】电影票房数据分析(二)数据可视化

    目录 图1 每年的月票房走势图 图2 年票房总值.上映影片总数及观影人次 图3 单片总票房及日均票房 图4 单片票房及上映月份关系图 在上一部分<[python数据分析实战]电影票房数据分析(一 ...

  5. 解决Anki服务器同步问题:坚果云 & Floder sync (已测试)

    读前须知: 更新日期:2019-07-08 1.本教程面向对象为:有一定计算机知识人群 2.配合参考链接中的文章,共同食用. 3.已经过测试,可同步图片,音频,视频 4.尝试有风险,提前导出Anki卡 ...

  6. 从零开始搭建Electron+Vue+Webpack项目框架,一套代码,同时构建客户端、web端(一)

    摘要:随着前端技术的飞速发展,越来越多的技术领域开始被前端工程师踏足.从NodeJs问世至今,各种前端工具脚手架.服务端框架层出不穷,“全栈工程师”对于前端开发者来说,再也不只是说说而已.在NodeJ ...

  7. electron调用c#动态库

    electron调用c#动态库 新建C#动态库 方法要以异步任务的方式,可以直接包装,也可以写成天然异步 代码如下 public class Class1 { public async Task< ...

  8. uni-app 请求封装

    1.创建一个http.js ​ const baseUrl = 'http://192.168.1.188:8080'; const httpRequest = (opts, data) => ...

  9. 像使用SQL一样对List对象集合进行排序

    在开始之前,我先卖个关子提一个问题:假设我们有一个Movie类,这个类有三个成员变量分别是starred(是否收藏), title(电影名称), rating(评分).你知道怎么对一个Movie对象组 ...

  10. 使用animate.css

    今天有个一前辈来看了一下我的小程序啊,说写的还行就是可以不用只按照ui给的图写界面,自己可以添加一些动态的炫酷效果,不用不知道一用吓一跳啊,用之前觉得好好一个界面为什么要搞那些花里胡哨的东西,单纯一点 ...