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. 设计糟糕的 RESTful API 就是在浪费时间!

    现在微服务真是火的一塌糊涂.大街小巷,逢人必谈微服务,各路大神纷纷忙着把自家的单体服务拆解成多个Web微小服务.而作为微服务之间通信的桥梁,Web API的设计就显得非常重要. HTTP是目前互联网使 ...

  2. 常用函数-Time

    #pragma pack(push,1) /* 在这中间定义的结构体,已单字节对齐 */ #pragma pack(pop) /************************************ ...

  3. linux使用jq工具解析json

    jq类似一个awk或grep一样的神器,可以方便地在命令行操作json 这里我使用海南万宁的天气接口做演示,地址:http://t.weather.sojson.com/api/weather/cit ...

  4. phpstorm安装步骤是什么?

    phpstorm的安装及其激活教程 1.phpstorm安装步骤: (1)下载地址:http://www.jetbrains.com/phpstorm/ 根据自己电脑的32or64位下载,下载完后就是 ...

  5. 04jmeter-Concurrency Thread Group

    1.下载插件Custom Thread Groups:参照:00jmeter安装相关 2.添加并发线程组 场景举例: 10个线程2分钟的加速时间5个加速步骤持有目标速率2分钟: 即: 2分钟除以5步, ...

  6. “无处不在” 的系统核心服务 —— ActivityManagerService 启动流程解析

    本文基于 Android 9.0 , 代码仓库地址 : android_9.0.0_r45 系列文章目录: Java 世界的盘古和女娲 -- Zygote Zygote 家的大儿子 -- System ...

  7. PostgreSQL使用安装

    PostgreSQL使用安装 一. 安装 ubuntu安装: # 安装客户端 sudo apt-get install postgresql-client # 安装服务器 sudo apt-get i ...

  8. 玩转OneNET物联网平台之MQTT服务④ —— 远程控制LED(设备自注册)+ Android App控制

    授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...

  9. 用最复杂的方式学会数组(Python实现动态数组)

    Python序列类型 在本博客中,我们将学习探讨Python的各种"序列"类,内置的三大常用数据结构--列表类(list).元组类(tuple)和字符串类(str). 不知道你发现 ...

  10. datatable dataset

    简单讲解一下dataset和datatable,以excel对比,dataset相当于一个excel文件,datatable相当于excel的一张表格.datatable可以单独应用,dataset里 ...