POJ2492:A Bug's Life(种类并查集)
A Bug's Life
| Time Limit: 10000MS | Memory Limit: 65536K | |
| Total Submissions: 45757 | Accepted: 14757 |
题目链接:http://poj.org/problem?id=2492
Description:
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.
Input
Output
Sample Input
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Sample Output
Scenario #1:
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!
Hint
题意:
给出m对关系,他们是互斥的,问给出的关系是否有矛盾。
题解:
种类并查集,可以用带权并查集解决,这里我就多开了两个集合。
思路可以参见我之前的一篇博客:https://www.cnblogs.com/heyuhhh/p/9980330.html
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; const int N = ;
int t,n,m;
int f[N]; int find(int x){
return f[x]==x ? f[x] :f[x]=find(f[x]);
}
bool same(int x,int y){
return find(x)==find(y);
}
void Union(int x,int y){
int fx=find(x),fy=find(y);
f[fx]=fy;
}
int main(){
scanf("%d",&t);int tot=;
while(t--){
tot++;
bool flag=true;
scanf("%d%d",&n,&m);
for(int i=;i<=N-;i++) f[i]=i;
for(int i=,x,y;i<=m;i++){
scanf("%d%d",&x,&y);
if(!flag) continue ;
int fx=find(x),fy=find(y);
if(!same(x,y) && !same(x+n,y+n)){
Union(x,y+n);Union(x+n,y);
}else flag=false;
}
printf("Scenario #%d:\n",tot);
if(flag) puts("No suspicious bugs found!");
else puts("Suspicious bugs found!");
printf("\n");
}
return ;
}
POJ2492:A Bug's Life(种类并查集)的更多相关文章
- POJ2492 A Bug's Life —— 种类并查集
题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Su ...
- 【POJ】2492 A bug's life ——种类并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28211 Accepted: 9177 De ...
- poj2492 A Bug's Life【并查集】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assume ...
- hdoj 1829 A bug's life 种类并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...
- HDU 1829 A Bug's Life(种类并查集)
思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...
- hdu1829A Bug's Life(种类并查集)
传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- A Bug's Life(种类并查集)(也是可以用dfs做)
http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit:5000MS Memory Limit:327 ...
- HDU 1829 A Bug's Life (种类并查集)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Oth ...
随机推荐
- 函数名前加 & 符号的深入理解 C++
#include <iostream> using namespace std; int& test_str() { ; return a; //通过返回 a 的地址来进行 值的返 ...
- 关于PHP性能提升踩过的一些坑
性能这个东西,在网站规模到达一定程度后,会是一个永恒的主题.关于这方面,本人有一些拙见,现在拿出来,大家一起探讨下. 1.编码过程中,传递参数时,尽量少使用‘引用传参’.这是一个巨坑啊 ...
- mysql学习第三天练习(多表连接)
-- 多表连接 -- 写一条查询语句,查询员工姓名.部门名称.工作地点 select ename,dname,loc from emp,dept where emp.deptno = dept.dep ...
- PHP.37-TP框架商城应用实例-后台13-商品管理-扩展分类的添加、显示【数据分组】、搜索分类【多对多】
商品扩展分类 需求:一件商品能有多个扩展分类,搜索任何一个分类都能搜出该商品 建表[扩展分类表] drop table if exists p39_goods_cat; create table p3 ...
- P2340 奶牛会展(状压dp)
P2340 奶牛会展 题目背景 奶牛想证明它们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N 头奶牛进行 了面试,确定了每头奶牛的智商和情商. 题目描述 贝西有权选择让哪些奶牛参加展览.由 ...
- Migrating from MapReduce 1 (MRv1) to MapReduce 2 (MRv2, YARN)...
This is a guide to migrating from Apache MapReduce 1 (MRv1) to the Next Generation MapReduce (MRv2 o ...
- 玩转VIM之将Vim全副武装
玩转VIM之将Vim全副武装 懒癌末期的我貌似很久没有写博客了,已经欠了多少篇在计划中的博客我已然不好意思说了.好了,言归正传,在前三篇介绍了Vim作为代码编辑器之后可能会有人说,要学习那么多指令真的 ...
- Android Google Maps 监听地图缩放
接上篇.http://www.cnblogs.com/maomishen/p/3556297.html 由于公司项目要求,需要对google map监听地图的缩放(zoom)来进行一些操作. 但是在网 ...
- Oracle 学习笔记(四)
oracle表查询 使用逻辑操作符号 查询工资高于 500 或者是岗位为 MANAGER 的雇员,同时还要满足他们的姓名首字母为大写 J SELECT * FROM emp WHERE (sal ...
- 「暑期训练」「基础DP」 Piggy-Bank (HDU-1114)
题意与分析 完全背包问题. 算法背包九讲里面都有提到过,我自己再说下对完全背包的理解. 为什么01背包中遍历状态从VV到00?考虑一下基本方程$dp[i][j]=max(dp[i-1][j-w[i]] ...