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 ...
随机推荐
- python(三):函数
一.函数.名称空间与作用域 1.函数的构成 python有三种层次的抽象:(1)程序可分成多个模块:(2)每个模块包含多条语句:(3)每条语句对对象进行操作.函数大致处于第二层.函数有它的定义格式.参 ...
- python笔记-20 django进阶 (model与form、modelform对比,三种ajax方式的对比,随机验证码,kindeditor)
一.model深入 1.model的功能 1.1 创建数据库表 1.2 操作数据库表 1.3 数据库的增删改查操作 2.创建数据库表的单表操作 2.1 定义表对象 class xxx(models.M ...
- MySQL数据库命名规范及约定
一.[操作规范]1. 如无备注,则表中的第一个id字段一定是主键且为自动增长:2. 如无备注,则数值类型的字段请使用UNSIGNED属性:3. 如无备注,排序字段order_id在程序中默认使用降序排 ...
- BZOJ4543 [POI2014]Hotel加强版
题意 有一个树形结构,每条边的长度相同,任意两个节点可以相互到达.选3个点.两两距离相等.有多少种方案? 数据范围:n<=100000 分析 参照小蒟蒻yyb的博客. 我们先考虑一个\(O(n^ ...
- ECMALL模板解析机制.MVC架构分析及文件目录说明.二次开发指南手册(转)
ECMALL模板解析语法与机制 http://www.nowamagic.net/architecture/archt_TemplateSyntaxAndAnalysis.php ECMALL模块开发 ...
- Java 设计模式之抽象工厂模式(三)
原文地址:Java 设计模式之抽象工厂模式(三) 博客地址:http://www.extlight.com 一.前言 上篇文章 <Java 设计模式之工厂模式(二)>,介绍了简单工厂模式和 ...
- volatile浅析
volatile的意思是不稳定的.易变的,但在多线程中却跟字面意思一毛钱关系没有.作为一个变量修饰符,它主要有两个作用:一个是告诉大家,该变量是一个在多个线程之间均可见的变量:另一个是告诉java虚拟 ...
- 基于Oracle的EntityFramework的WEBAPI2的实现(一)——准备工作
目前在.net的范围内,好的而且方便的ORM的真的不是很多,与VS集成方便的也就当属EntityFramework(以下简称EF,不知道为什么,总EF这个缩写好不专业).但是,好多公司使用的又是ORA ...
- laravel 中事务的使用
在laravel5.3中使用事务 可以直接使用\DB::beginTransaction();使用 前提是数据库表必须支持事务,目前mysql中表类型只有InnoDb支持事务 想要在一个数据库事务中运 ...
- 给DB2增加删除字段二三事
加字段用这个,CS.TG表名,LINE2_TYPE字段名,CHAR(1)字段类型 ALTER TABLE CS.TG ADD COLUMN LINE2_TYPE CHAR(1); 要是加错了用以下语句 ...