A Bug's Life(加权并查集)
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
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
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
#include <iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<algorithm>
typedef long long ll;
const int MAXN=5e5+;
int m,n,flag;
int str[MAXN];
int pre[MAXN];
using namespace std;
void init(int l)
{
for(int i=; i<=l; i++)
{
str[i]=i;
pre[i]=;
}
return;
}
int Find(int x)
{
if(x==str[x])
return str[x];
int temp=Find(str[x]);//递归法求根节点并更新到根节点之间的距离
pre[x]=(pre[x]+pre[str[x]])%;
str[x]=temp;
return str[x];
}
void Union(int x,int y)
{ int root1=Find(x);
int root2=Find(y);
if(root1==root2)//根节点相同
{
if((pre[x]==pre[y]))//与根节点同性
flag=;
}
str[root1]=root2;
pre[root1]=(abs(pre[x]-pre[y])+)%;//更新两个根节点之间的距离 }
int main()
{
int p,k,u,r=,l;
cin>>m;
while(m--)
{ flag=;
cin>>l>>k;
init(l);
for(int i=; i<=k; i++)
{
scanf("%d%d",&p,&u);
if(flag)
continue;
Union(p,u);
}
printf("Scenario #%d:\n",r++);
if(flag) printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
printf("\n"); }
return ;
}
A Bug's Life(加权并查集)的更多相关文章
- A Bug's Life(加权并查集)
Description Background Professor Hopper is researching the sexual behavior of a rare species of bug ...
- hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14
题意: 有一个运动场,运动场的坐席是环形的,有1~300共300列座位,每列按有无限个座位计算T_T. 输入: 有多组输入样例,每组样例首行包含两个正整数n, m.分别表示共有n个人,m次操作. 接下 ...
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- HDU 3407.Zjnu Stadium 加权并查集
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- P1196 银河英雄传说(加权并查集)
P1196 银河英雄传说 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在 ...
- Zjnu Stadium(加权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 洛谷 P2024 [NOI2001]食物链(种类并查集,加权并查集)
传送门 解题思路 加权并查集: 什么是加权并查集? 就是记录着每个节点到它的父亲的信息(权值等). 难点:在路径压缩和合并节点时把本节点到父亲的权值转化为到根节点的权值 怎么转化呢? 每道题都不一样Q ...
- UVALive 4487 Exclusive-OR 加权并查集神题
已知有 x[0-(n-1)],但是不知道具体的值,题目给定的信息 只有 I P V,说明 Xp=V,或者 I P Q V,说明 Xp ^ Xq=v,然后要求回答每个询问,询问的是 某任意的序列值 Xp ...
- 牛客网-Beauty of Trees 【加权并查集】
锟斤拷锟接o拷https://www.nowcoder.com/acm/contest/119/A锟斤拷源锟斤拷牛锟斤拷锟斤拷 锟斤拷目锟斤拷锟斤拷 It锟斤拷s universally acknow ...
随机推荐
- JPA无法删除对象【实际项目解决办法】
并非通用, 根据自己实际情况来 不能删除前的dao方法 public void delete(CmsProjectNew bean); 可以删除后的dao方法 @Modifying @Query(&q ...
- onerror="javascript:this.src='images/defaultUpload.png';"引发的死循环错误
18:10:47.441 WARN o.s.web.servlet.PageNotFound:1101 - No mapping found for HTTP request with URI [/c ...
- 每天一个linux命令:【转载】rm命令
今天学习一下linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除 ...
- BZOJ3566 SHOI2014 概率充电器 【概率DP】
BZOJ3566 SHOI2014 概率充电器 Description 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品——概率充电器: “采用全新纳米级加工技术,实现元件与导线能 ...
- 《DSP using MATLAB》示例Example6.2
2017年了,阳历新年都11号了,已从外地回到家乡,依然苦逼的生活…… 接着写读书(Digital Signal Processing using MATLAB)笔记吧 代码: b = [1 -3 1 ...
- ubuntu qq安装
安装最新版qq2016 qq2012下载链接: https://pan.baidu.com/s/1miFVc04 密码: 3g9w 先解压到自己所在的目录,解压命令自己百度在这个目录下,有三个解压包, ...
- REST服务开发实战【转】
原文:http://kb.cnblogs.com/page/91827/ REST介绍 如果要说什么是REST的话,那最好先从Web(万维网)说起. 什么是Web呢?读者可以查看维基百科的词条(htt ...
- python string 之 format, join, split
功能太强大. 经常看到很多简洁, 高级的用法. 但是基本思路是{}代替了以前的%. In [1]: '{0},{1}'.format('kzc',18) Out[1]: 'kzc,18' In [2] ...
- MySQL-Proxy 读写分离、同步延时问题解决方案
MySQL-Proxy 读写分离.同步延时问题解决方案 使用MySQL将读写请求转接到主从Server. 一 安装MySQL Proxy MySQL Proxy的二进制版非常方便,下载解压缩后即用. ...
- 怎样创建Linux Initrd
Linux初始RAM磁盘(initrd)是在系统引导过程中挂载的一个暂时根文件系统,用来支持两阶段的引导过程.initrd文件里包括了各种可运行程序和驱动程序.它们能够用来挂载实际的根文件系统,然后再 ...