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 ...
随机推荐
- NotifyIcon控件
NotifyIcon就是系统托盘控件.通知区域中的图标是一些进程的快捷方式,这些进程在计算机后台运行,如防病毒程序或音量控制.这些进程不会具有自己的用户界面.NotifyIcon 类提供了编写此功能的 ...
- Web.config配置文件中的属性add,key,value含义
这是添加自定义字符串的方式,保存是以键-值的形式保存的,可以通过key获取value,一般用这种方法配置全局内使用的字符串. <configuration>是配置文件的根配置节. < ...
- 【题解】HAOI2007分割矩阵
水题盛宴啦啦啦……做起来真的极其舒服,比某些毒瘤题好太多了…… 数据范围极小 --> 状压 / 搜索 / 高维度dp:观察要求的均方差,开始考虑是不是能够换一下式子.我们用\(a_{x}\)来表 ...
- [NOI.AC省选模拟赛3.23] 集合 [数学]
题面 传送门 一句话题意: 给定$n\leq 1e9,k\leq 1e7,T\leq 1e9$ 设全集$U=\lbrace 1,2,3,...n\rbrace $,求$(min_{x\in S}\lb ...
- 洛谷 [SDOI2015]约数个数和 解题报告
[SDOI2015]约数个数和 题目描述 设\(d(x)\)为\(x\)的约数个数,给定\(N,M\),求$ \sum\limits^N_{i=1}\sum\limits^M_{j=1}d(ij)$ ...
- 创建dll
在制作dll的时候,如果全局变量不导出,而函数调用中,包含了全局变量,那么会出现全局变量没有值的问题. add.c #pragma once //强制无签名utf-8 #include "a ...
- 自定义toolbar教程
1.写toolbar的布局文件 ,toolbar.xml <?xml version="1.0" encoding="utf-8"?> <Re ...
- hdu 1520Anniversary party 树形dp入门
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...
- 2016"百度之星" - 初赛(Astar Round2A)--HDU 5690 |数学转化+快速幂
Sample Input 3 1 3 5 2 1 3 5 1 3 5 99 69 Sample Output Case #1: No Case #2: Yes Case #3: Yes Hint ...
- jquery with ajax
with session storage: 1.ajax请求可以放在 $(document).ready(function (){...}); 里. 2. $.ajax({ url: "/a ...