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条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令 ...
随机推荐
- 开坑,Unix环境高级编程,转行之路又得缓缓了
不要问我基础,我用了近6年的Linux系统,最早的版本可以追溯到Ubuntu 8.04,常用的命令 VIM基本上是没压力,遇到问题google 配置环境变量 网络环境也不在话下, C语法基本熟练,过去 ...
- Redis缓存数据库详解
Redis最为常用的数据类型主要有以下五种: 1)String 2)Hash 3)List 4)Set 5)Sorted set 在具体描述这几种数据类型之前,我们先通过一张图了解下Redis内部内存 ...
- Css 常用属性
1. overflow:hidden和zoom:1 verflow:hidden;的作用 1. 隐藏溢出 :2.消除浮动 <style type="text/css"> ...
- FusionCharts或其它flash的div图层总是浮在最上层? (转)
div的图层由div的style中的z-index来决定,z-index是层垂直屏幕的坐标,0最小,越大的话位置越靠上. 由于FusionCharts的图表都放在div中,如果页面还有其他的div,将 ...
- slf4j,log4j,logback 初步使用
log4j,slf4j,logback简单介绍见 LogBack简易教程 Logback浅析 简单的将,slf4j是一个日志的框架,有各种日志的接口,但是并不包含实际的写日志的方法. log4j,lo ...
- easyui-combobox的取值问题
例子:<select id="cc" class="easyui-combobox" name="cc" style="wi ...
- matlab中textread
今天打算跑下程序,突然发现,真的很烂,不会读入数据,简单的Iris.txt一上午都没读进去,在此对matlab中的textread函数做下总结,textscan函数待续. 本文主要内容引自http:/ ...
- 通过数据库方式访问excel 2007及其以后(xlsx)文件的连接字符串
sqlconn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test3.xls;Extended Properties=&q ...
- C#脚本引擎 CS-Script 之(三)——如何部署
本文不但介绍了CS-Script如何部署,还介绍了CS-Script的部署后面的原理,并用一个框图详细介绍了部署中的各种细节. 一.获取资源 1.从官网上下载编译好的csscript资源:cs-scr ...
- Linux(9.28-10.4)学习笔记
三种数字表示 无符号数: 基于传统的二进制表示法,表示大于或者等于零的数字. 补码(有符号数): 表示有符号数整数的最常见的方式,有符号数就是只可 以为正或者为负的数. 浮点数: 表示实数的科学计数法 ...