POJ 2492 A Bug's Life 并查集的应用
题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突。
思路:用并查集,每次输入一对虫子后就先判定一下。如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突。否则就将两只虫子并入一个集合。
而性别则是用一个gender数组来维护,每个虫子的gender的值为0或者1。
0表示该虫子的性别与父亲结点的性别相同。
1表示该虫子的性别与父亲结点的性别不同。
这题和poj1703本质上是一样的。1703的题解请点传送门。
另外还有一个疑惑,本题输入量巨大,我用输入加速后反而比用scanf要慢得多,弄不明白为什么。。有知道的大神欢迎来给解答一下。
#include<stdio.h>
#define maxn 2010
int father[maxn], gender[maxn];
int Find(int x)
{
if (father[x] != x)
{
int t = father[x];
father[x] = Find(father[x]);
gender[x] = (gender[x] + gender[t]) % ;
}
return father[x];
}
void Merge(int x,int y)
{
int fx = Find(x);
int fy = Find(y);
father[fx] = fy;
if (gender[y] == )
gender[fx] = ^ gender[x];
else gender[fx] = gender[x];
}
int main()
{
int t;
int cas = ;
//freopen("data.in", "r", stdin);
scanf("%d",&t);
while (t--)
{
int n, m;
scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++)
{
father[i] = i;
gender[i] = ;
}
int ok = ;
while (m--)
{
int a, b;
scanf("%d%d",&a,&b);
if (!ok) continue;
if (Find(a) != Find(b))
Merge(a, b);
else if(gender[a] == gender[b])
ok = ;
}
if (ok) printf("Scenario #%d:\nNo suspicious bugs found!\n\n", cas++);
else printf("Scenario #%d:\nSuspicious bugs found!\n\n", cas++);
}
return ;
}
POJ 2492 A Bug's Life 并查集的应用的更多相关文章
- nyoj 209 + poj 2492 A Bug's Life (并查集)
A Bug's Life 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Background Professor Hopper is researching th ...
- 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 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 (并查集)
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 ...
- poj 2492 A Bug's Life 二分图染色 || 种类并查集
题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...
随机推荐
- Java8_Lambda表达式
从2014年java8发布到现在已经有几个年头了,现在java11都发布了.公司最近把服务器环境重新搭建了一遍,jdk版本也从7换成了8,终于可以在代码里面写Lambda表达式了.作为 ...
- 线性回归、逻辑回归(LR)
线性回归 回归是一种极易理解的模型,就相当于y=f(x),表明自变量 x 和因变量 y 的关系.最常见问题有如 医生治病时的望.闻.问.切之后判定病人是否生了什么病,其中的望闻问切就是获得自变量x,即 ...
- tomcat内存泄漏存入dump文件
很多tomcat进程退出(或者进程假死),都是由于频繁的抛出OutOfMemeoryError导致的. 为了让tomcat退出前或者发生OutOfMemeoryError时自动dump堆栈信息,方便事 ...
- Pre 自动换行和手动换行
pre { white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre ...
- ios开发学习笔记040-autolayout 第三方框架Masonry
不管是是界面创建约束还是代码创建约束,苹果官方提供的方式都比较繁琐.所以出现了第三方框架. Masonry 在github地址如下: https://github.com/SnapKit/Masonr ...
- Monkey日志信息的11种Event percentages
我们查看官方文档,表里只给出了8种事件(可以看我上篇的翻译文档).但我们运行Monkey后,却发现有11种事件!最坑爹的是,在每种事件的百分比后面,他还不给注明是什么事件! 原来不同的Android ...
- python - 接口自动化测试 - TestLogin - 登录接口测试用例
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_login.py @ide: PyCharm C ...
- 深入学习之mysql(一)数据库操作
1.显示所有数据库: SHOW DATABASES; 2.创建数据库: CREATE DATABASE 数据库名: 3.选择你所创建的数据库: USE 数据库名; 4.删除数据库: DROP 数据库名 ...
- [报错处理]Could not find a version that satisfies the requirement xml (from versions)
安装xml库发生报错 pip3 install xml Collecting xml Could not find a version that satisfies the requirement x ...
- Sina微博OAuth2框架解密
自从sina微博oauth2出来以后, 第三方集成开发简单了很多. Oauth2不像oauth1一样需要后台httpclient请求那么麻烦, 一切都可以在前台使用ajax实现了. 很多人觉得蹊跷, ...