poj2492--A Bug's Life(并查集变形)
| Time Limit: 10000MS | Memory Limit: 65536K | |
| Total Submissions: 28703 | Accepted: 9350 |
Description
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
space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
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
Source
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int c[2100] , d[2100] ;
int find1(int a)
{
if( c[a] != a )
{
c[a] = find1(c[a]) ;
d[a] = d[ c[a] ] ;
}
return c[a] ;
}
int main()
{
int t , tt , i , j , n , m , a , b , flag ;
scanf("%d", &t);
for(tt = 1 ; tt <= t ; tt++)
{
scanf("%d %d", &n, &m);
for(i = 1 ; i <= n ; i++)
c[i] = i ;
memset(d,-1,sizeof(d));
flag = 0 ;
while(m--)
{
scanf("%d %d", &a, &b);
if( flag ) continue ;
int x , y ;
x = find1(a) ; y = find1(b) ;
if(x == y)
flag = 1 ;
else if( d[x] == -1 && d[y] == -1 )
{
d[x] = y ; d[y] = x ;
}
else if( d[x] != -1 )
{
c[y] = d[x] ;
if( d[y] != -1 )
{
int xx = find1(d[y]);
c[xx] = x ;
d[xx] = y ;
}
d[y] = x ;
}
else
{
c[x] = d[y] ;
if( d[x] != -1 )
{
int yy = find1(d[x]);
c[yy] = y ;
d[yy] = x ;
}
d[x] = y ;
}
}
printf("Scenario #%d:\n", tt);
if( flag )
printf("Suspicious bugs found!\n\n");
else
printf("No suspicious bugs found!\n\n");
}
return 0;
}
poj2492--A Bug's Life(并查集变形)的更多相关文章
- poj2492 A Bug's Life【并查集】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assume ...
- POJ2492 A Bug's Life —— 种类并查集
题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Su ...
- hdu1829 A Bug's Life(并查集)
开两个并查集.然后合并的时候要合并两次.这样在合并之前推断是否冲突,假设不冲突就进行合并,否则不须要继续合并. #include<cstdio> #include<cstdlib&g ...
- 【POJ】2492 A bug's life ——种类并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28211 Accepted: 9177 De ...
- POJ 2492 A Bug's Life (并查集)
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- A Bug's Life(加权并查集)
Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...
- POJ_2492 A Bug's Life 【并查集】
一.题面 POJ2492 二.分析 并查集判断类别的题目感觉套路都差不多. 还是先判断在不在一个集合里,在一个集合里才能判断是否同类. 若不在一个集合里则需要将这两个点联系起来. 关于联系起来后关系的 ...
- POJ 2492 A Bug's Life【并查集高级应用+类似食物链】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- HDU-1829 A Bug's Life。并查集构造,与POJ1709异曲同工!
A Bug's Life Find them, Catch them 都是并查集构造的题,不久前 ...
随机推荐
- 【零基础入门学习Python笔记013】元祖:戴上了枷锁的列表
元组:戴上了枷锁的列表 因为和列表是近亲关系.所以元祖和列表在实际使用上是很相似的. 本节主要通过讨论元素和列表究竟有什么不同学习元祖. 元组是不可改变元素的.插入.删除或者排序都不能够.列表能够随意 ...
- 【ASP】在特定的范围内产生N个不同的随机数
ASP产生一个随机数不难.就两条特定语句: <% Randomize x=int(20*rnd+1) %> 以上的两条语句.表示从1~20这个范围内产生随机数,而且这些随机数都是整数. 那 ...
- 基于FPGA的VGA可移植模块终极设计
一.VGA的诱惑 首先,VGA的驱动,这事,一般的单片机是办不到的:由于FPGA的速度,以及并行的优势,加上可现场配置的优势,VGA的配置,只有俺们FPGA可以胜任,也只有FPGA可以随心所欲地配置( ...
- DNS查询报文实例
2.2 DNS查询报文实例 以www.baidu.com为例,用Wireshark俘获分组,结合2.1的理论内容,很容易看明白的,DNS请求报文如下: 图7 DNS请求报文示例 2.3 DNS回答报文 ...
- 【转】如何在Mac 终端升级ruby版本
原文网址:https://segmentfault.com/a/1190000003784636 rvm是什么?为什么要安装rvm呢,因为rvm可以让你拥有多个版本的Ruby,并且可以在多个版本之间自 ...
- 求包含每个有序数组(共k个)至少一个元素的最小区间
title: 求包含每个有序数组(共k个)至少一个元素的最小区间 toc: false date: 2018-09-22 21:03:22 categories: OJ tags: 归并 给定k个有序 ...
- line-height与间距总总
一点说明(个人吐槽,可以略过) 之所以想写这篇文章,是因为自己工作的经验总结.以前的页面编写极度不注重间距大小,特别是行级元素间距.认为只要差不多好就行了,没必要花那么大的精力去抠几px的小细节.事实 ...
- 使用最新vue_cli+webpack搭建的模版
使用最新vue_cli+webpack搭建的模版,包含了常用的插件,router和axiox与测试插件.项目的结构如下: 框架下载地址:https://share.weiyun.com/5Cl7EbU
- .net 三大核心对象
.net 三大核心对象 HttpRequest 现在总算轮到第一个核心对象出场了.MSDN给它作了一个简短的解释:“使 ASP.NET 能够读取客户端在 Web 请求期间发送的 HTTP 值.”这个解 ...
- apache include 文件包含引用的方法 报错 [an error occurred while processing this directive]
今天遇到在某平台买的虚拟主机服务器不支持 下面的这样的写法 <!--#Include file="/templets/2013new/header.htm"--> ...