POJ 2492 A Bug's Life【并查集高级应用+类似食物链】
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.
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
在他的试验里,每个虫子和它的性行为都很容易辨认,因为它们的背后印着号码。
给出一些虫子的性行为,确定是否有同性恋的虫子能推翻这个假设。某物种n只,m个描述,表示x,y为异性,判断是否有同性交配。如果发现存在同性交配输出bugs found.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string> using namespace std;
const int maxn = ;
int fa[maxn],r[maxn];
int n,m,a,b;
void init()
{
for(int i=;i<=n;i++)
{
fa[i]=i;
r[i]=;
}
} int find(int x)
{
if(fa[x]==x) return x;
else
{
int rt=find(fa[x]);
// fa[x]=find(fa[x]);
r[x]=r[x]^r[fa[x]];
return fa[x]=rt;
}
return fa[x];
} void join(int x,int y)
{
int fx=find(x);
int fy=find(y);
fa[fx]=fy;
r[fx]= (!(r[y]^r[x]));
/*fa[fy]=fx;
r[fy]=(1+r[y]+r[x])%2;*/
} int main()
{
int t,flag;
int cas=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();//注意初始化!
flag=;
for(int i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
if(find(a)!=find(b))//根不同就合并
{
join(a,b);
}
else
{
if(r[a]==r[b])//r[x]=r[y]可推出x和y同性,那么就说明存在bug。详见代码:
flag=;
}
if(flag) continue;
} printf("Scenario #%d:\n",++cas); if(flag)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n"); printf("\n");
}
}
位运算版类别偏移
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 并查集的应用
题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...
- POJ 2492 A Bug's Life (并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 30130 Accepted: 9869 De ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
- A Bug's Life POJ - 2492 (种类或带权并查集)
这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...
- 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 ...
- 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 ...
随机推荐
- 【bzoj1977】[BeiJing2010组队]次小生成树 Tree 最小生成树+权值线段树合并
题目描述 求一张图的严格次小生成树的边权和,保证存在. 输入 第一行包含两个整数N 和M,表示无向图的点数与边数. 接下来 M行,每行 3个数x y z 表示,点 x 和点y之间有一条边,边的权值为z ...
- HDU 6208 The Dominator of Strings(AC自动机)
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- 《软件调试的艺术》学习笔记——GDB使用技巧摘要
<软件调试的艺术>学习笔记——GDB使用技巧摘要 <软件调试的艺术>,因为名是The Art of Debugging with GDB, DDD, and Eclipse. ...
- poj1185 炮兵阵地 状压dp
司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用"P"表示) ...
- Planning your upgrade with Upgrade Advisor
Planning your upgrade with Upgrade Advisor You should use the Upgrade Advisor tool (if it is availab ...
- 知问前端——日历UI(一)
日历(datepicker)UI,可以让用户更加直观的.更加方便的输入日期,并且还考虑不同国家的语言限制,包括汉语. 调用datepicker()方法 $('#date').datepicker(); ...
- css划斜线
http://stackoverflow.com/questions/18012420/draw-diagonal-lines-in-div-background-with-css
- NGINX : 如何屏蔽未被定义的虚拟主机的访问
参考: [ how to prevent undefined server names ] nginx 的默认虚拟主机 Nginx 支持基于域名和端口的虚拟主机(virtual host), 根据获取 ...
- POJ 1002 487-3279 (map )
title: 487-3279 map POJ1002 tags: [map] 题目链接 Description Businesses like to have memorable telephone ...
- bzoj 1005 组合数学 Purfer Sequence
这题需要了解一种数列: Purfer Sequence 我们知道,一棵树可以用括号序列来表示,但是,一棵顶点标号(1~n)的树,还可以用一个叫做 Purfer Sequence 的数列表示 一个含有 ...