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 ...
随机推荐
- NIO Q&A(持续补充。。。。)
Q:NIO是非阻塞的.但调用的selector.select()方法会阻塞.这和NIO非阻塞岂不是矛盾了? A:非阻塞指的是 IO 事件本身不阻塞,但是获取 IO 事件的 select 方法是需要阻塞 ...
- NOIP2018复赛 游记
Day -? 停了两个星期的课,逃了一场期中考试.随便做了点题并不知道有什么用.见不到夫人很难受. Day 0 依然没有跟学校走.据说今年合肥居然不下雨,印象里每年这个时候来到这里都是阴雨连绵……突然 ...
- [Leetcode] 3sum-closest 给定值,最为相近的3数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 洛谷 P2529 [SHOI2001]击鼓传花 解题报告
P2529 [SHOI2001]击鼓传花 题意:求出\(n!\)末尾最后一位非0数字 数据范围:\(n<=10^{100}\) 我们从简单的开始考虑 1.显然,\(n!\)可以被这么表示 \(n ...
- selenium 获取某个元素的html
<table> <tbody id="tb-37327761306"> <tr class="sep-row"><td ...
- Windows2008 – Task Scheduler – ‘Action “C:\Windows\SYSTEM32\cmd.exe” with return code 1’
Remediation Edit Task Let us make the necessary changes, which is to specify the Start folder. Here ...
- Java之戳中痛点 - (3)三目运算符的两个操作数类型尽量一致
先看一个例子: package com.test; public class TernaryOperator { public static void main(String[] args) { in ...
- 解决在ios下不能自动播放音频的问题
在ios下面是不允许自动播放音频这个操作的,不过我们可以使用微信提供的sdk来做到自动播放音乐的功能 wx.ready(()=>{}) 引入微信的sdk,之后在回调函数里面执行主动触发背景音乐播 ...
- Win7/8 绿色软件开机启动
在查找番茄工作法PC端软件时,发现了淡高的文章win8绿色软件开机启动,试用了一下wintabs,的确好用! 另外,office软件中有一款 OFFICE tabs的插件,标签式的管理,非常方便快捷, ...
- 【Foreign】猜测 [费用流]
猜测 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 3 1 1 1 2 2 1 Samp ...