nyoj 209 + poj 2492 A Bug's Life (并查集)
A Bug's Life
- 描述
- 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 (并查集)的更多相关文章
- POJ 2492 A Bug's Life (并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 30130 Accepted: 9869 De ...
- 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,二分染色。
Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...
- 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 ...
随机推荐
- 常用函数-String
/************************************************************************ 函数功能:将字符串中str的old_value子字符 ...
- 可实现的全局唯一有序ID生成策略
在博客园搜素全局唯一有序ID,罗列出来的文章大致讲述了以下几个问题,常见的生成全局唯一id的常见方法 :使用数据库自动增长序列实现 : 使用UUID实现: 使用redis实现: 使用Twitter的 ...
- .NET Core System.Drawing.Common 中文乱码的坑
最近在写一个汉字取点阵的程序,最开始是在win环境下运行的,没发现什么异常,然后今天把程序放在centos 下后发现英文正常,中文完全变成两位的字了,最开始是字体的原因 在把宋体等安装到centos ...
- django-VIews之HttpResponse(一)
HttpResponse(content,conent_type=None,status=None,charset=None,*args,**kwargst) content:返回给视图的内容 con ...
- Connection activation failed Device not managed by NetworkManager
1)查看NetworkManager服务是否启动 ps aux |grep NetworkManager 使用service NetworkManager start 命令启动该网络管理程序 2) 一 ...
- 如何学好javascript
今天逛论坛时看到有朋友问,是否有专门教Javascript的学校,这里想想把自己的一点建议和自己3年来的前端Javascript开发的经验跟大家分享下,也给出几本个人认为不错的书来做为大家学习的参考资 ...
- Python3.8更新特性
Python 3.8.0稳定版 部分新特性: • PEP 572,赋值+表达式 :=可以将一个表达式或者一个 if (n := len(a)) > 10:#表达式仍然用,赋值也放一起,后面不用多 ...
- 【MySQL】MySQL使用正确密码却认证失败问题解决方法
前言:笔者根据 #MySQL忘记密码,重置密码方法 ,修改密码后.使用修改后的正确密码怎么也登录不上数据库,然后经过以下方法,重新登录数据库. 1.确认MySQL安装目录下没有data(Data)文件 ...
- 【aliyun】学java,看这里,不迷茫!1460道Java热门问题
阿里极客公益活动: 或许你挑灯夜战只为一道难题 或许你百思不解只求一个答案 或许你绞尽脑汁只因一种未知 那么他们来了,阿里系技术专家来云栖问答为你解答技术难题了 他们用户自己手中的技术来帮助用户成长 ...
- H5 + WebGL 实现的楼宇自控 3D 可视化监控
前言 智慧楼宇和人们的生活息息相关,楼宇智能化程度的提高,会极大程度的改善人们的生活品质,在当前工业互联网大背景下受到很大关注.目前智慧楼宇可视化监控的主要优点包括: 智慧化 -- 智慧楼宇是一个生态 ...