POJ2492 A Bug's Life
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 33833 | Accepted: 11078 |
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
first line of the input contains the number of scenarios. Each scenario
starts with one line giving the number of bugs (at least one, and up to
2000) and the number of interactions (up to 1000000) separated by a
single 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
output for every scenario is a line containing "Scenario #i:", where i
is the number of the scenario starting at 1, followed by one line saying
either "No suspicious bugs found!" if the experiment is consistent with
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
带偏移量的并查集。
两个集合并时,要注意处理dis数组,使点之间的关系不出错
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int mxn=;
int fa[mxn];
int dis[mxn];
int n,m;
bool flag;
int find(int x){
if(fa[x]==x){
return x;
}
int newfx=find(fa[x]);
dis[x]=(dis[x]+dis[fa[x]])%;
return fa[x]=newfx;
}
bool pd(int a,int b){
int u=find(a),v=find(b);
if(u==v){
if(dis[a]!=(dis[b])^){
flag=;
return ;
}
}
else{
fa[u]=v;
dis[u]=((dis[a]-dis[b])^);
}
return ;
}
void init(int x){
for(int i=;i<=x;i++)fa[i]=i,dis[i]=;
}
int main(){
int T;
scanf("%d",&T);
for(int ro=;ro<=T;ro++){
printf("Scenario #%d:\n",ro);
scanf("%d%d",&n,&m);
init(n);
int i,j;
int x,y;
flag=;
for(i=;i<=m;i++){
scanf("%d%d",&x,&y);
pd(x,y);
}
if(flag)printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
printf("\n");
}
return ;
}
POJ2492 A Bug's Life的更多相关文章
- [poj2492]A Bug's Life(并查集+补集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 34678 Accepted: 11339 D ...
- 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 题意 虫子有两种性别,有n只虫子,编号1~n,输入m组数据,每组数据包含a.b两只虫子,表示a.b为不同性别的虫子,根据输入的m ...
- POJ2492 A Bug's Life —— 种类并查集
题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Su ...
- POJ2492 A Bug's Life 带权并查集
分析:所谓带权并查集,就是比朴素的并查集多了一个数组,记录一些东西,例如到根的距离,或者和根的关系等 这个题,权数组为relation 代表的关系 1 和父节点不同性别,0,和父节点同性别 并查集一 ...
- poj2492 A Bug's Life【基础种类并查集】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298148.html ---by 墨染之樱花 题目链接:http://poj.org/pr ...
- hdu1829&&poj2492 A Bug's Life 基础种类并查集
把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续 #include <cstdio> #include <cstring> ...
- POJ2492 A Bug's Life 判断二分图
给一个无向图,判断是否为二分图 是否有奇环.用染色法解决,也可以用并查集解决,时间复杂度是相同的,但是并查集用的内存少. 这里给出Bfs染色的代码 #include<iostream> # ...
- POJ-2492 A Bug's Life(种类并查集)
http://poj.org/problem?id=2492 题意: 给出一个T代表几组数据,给出一个n一个m,代表人的编号由1~n,m条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令 ...
随机推荐
- ZIP文件伪加密
题目给出图片,那当然是从图片下手啦! 首先下载图片,在Linux系统下用binwalk工具打开,果然不出所料,里面藏有文件! 用dd把它分解出来! 'txt' 格式的文件提取出来!会看到一个Zip压缩 ...
- iOS9 判断微信qq是否安装
iOS 9检测QQ.微信是否安装无效的解决方法 在info.plist里面添加LSApplicationQueriesSchemes(Array类型),然后插入weixin, wechat, mqq的 ...
- tp登录方法
先根据输入的名字检测是否存在数据表中,如果有继续进一步验证密码,除了select(),find()可以查询数据外,还可以用getByXxx()指定数据表字段查询, 比如数据表有username字段,就 ...
- iOS中使用RSA对数据进行加密解密
RSA算法是一种非对称加密算法,常被用于加密数据传输.如果配合上数字摘要算法, 也可以用于文件签名. 本文将讨论如何在iOS中使用RSA传输加密数据. 本文环境 mac os openssl-1.0. ...
- 05JavaIO详解_仿照IO源码自己去实现一个IO流(为了加深印象,本身没有价值)
版本会越来越难: 版本1:只写一个read方法 package com.guigu.shen.InputStream; import java.io.IOException; import java. ...
- 自定义WPF ListBox的选中项样式
首先介绍一种简单地方法:就是通过自定义SystemColors类的参数来自定义WPF ListBox选择颜色的,SystemColors的HighlightBrushKey和HighlightText ...
- Page Security
参见开发文档 Overview This document describes how to build applications that grant selected access to indi ...
- memcached协议
memcached协议 旧版:http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt 新版:https://githu ...
- Asp.net MVC十问十答[译]
1. Explain MVC (Model-View-Controller) in general? MVC (Model-View-Controller) is an architectural s ...
- 在opencv3中实现机器学习算法之:利用最近邻算法(knn)实现手写数字分类
手写数字digits分类,这可是深度学习算法的入门练习.而且还有专门的手写数字MINIST库.opencv提供了一张手写数字图片给我们,先来看看 这是一张密密麻麻的手写数字图:图片大小为1000*20 ...