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条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令 ...
随机推荐
- Flex xml编辑器(老外写的)
github上的一个项目老外写的xml编辑器,灵活利用了Tree的labelFunction实现节点运行时展现.开源地址是 https://github.com/softinsure/XML-Edit ...
- WinForm 快捷键设置
一.窗体快捷键,只在窗体上有效果 首先在form_load的时候写上this.KeyPreview=true;//表示窗体接受按键事件 然后如下 private void Frm_KeyDown(ob ...
- FusionCharts V3图表导出图片和PDF属性说明(转)
百闻不如一见,狠狠点击,快快下载:(演示文档有错误,不提供下载了.待新的演示文档出来.) 许多朋友说上面的DEMO用不了.fusioncharts官方的演示非常不错,就是来不及整理,各位大侠们可以研究 ...
- ie6,ie7兼容性总结(转)
其实浏览器的不兼容,我们往往是各个浏览器对于一些标准的定义不一致导致的,因此,我们可以进行一些初始化,很多问题都很轻松解决. 下面是14条特殊情况仅供参考: 1. 文字本身的大小不兼容.同样是font ...
- SilverIight数据绑定实例
前台Code <DataGrid Name="DataGrid1" AutoGenerateColumns="False" IsReadOnly=&quo ...
- String类及常用方法
在学习String类之前,先看一道笔试题:new String("abc")创建了几个对象? 答案: 两个对象, 一个对象是 位于字符串常量池中,一个对象是位于堆内存中. 原因:主 ...
- 大前端时代已经到来!传智播客2015之WEB前端视频教程(全套教程共15G)
大前端时代已经到来!传智播客2015之WEB前端视频教程(全套教程共15G)大前端时代已经到来!如今,前端开发工程师的职责,不是只有切图.制作网页这么简单哦! G:\传智播客2015-WEB前端视频教 ...
- word 2010自定义快捷键提高工作效率
经常使用word处理文档, 做笔记的时候会把word文档框缩小,以便同时看pdf同时记录笔记,但是缩小的word框不能把所有的菜单项显示出来,我比较常用那个插入边框下面的那个横线来做分割符,但是缩小了 ...
- [CareerCup] 7.4 Implement Multiply Subtract and Divide 实现乘法减法和除法
7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...
- Android开发之高效加载Bitmap
一.概述 在Android开发中,我们经常与Bitmap打交道,而对Bitmap的不恰当的操作经常会导致OOM(Out of Memory).这篇文章我们会介绍如何高效地在Android开发中使用Bi ...