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 ...
随机推荐
- hihoCoder1599 bfs
特殊的剪枝,整体上和辗转相除法有点像 #1599 : 逃离迷宫4 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi被坏女巫抓进一座由无限多个格子组成的矩阵迷宫. 小 ...
- ajax完成团队信息异步添加【实际项目】
第一:ajax往后台传参如何串(目前理解是json数组直接传给对象) 第二:ajax返回的数值通过PrintWriter.print方法返回 [参考前台页面关于团队信息是如何实现的] 参考页面user ...
- BZOJ2763 JLOI2011 飞行路线 【最短路+DP】
BZOJ2763 JLOI2011 飞行路线 Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n ...
- iOS 5 :一个UIPageViewController程序示例
原文:http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_UIPageViewController_Application 在Xco ...
- Machine Schedule(二分图匹配之最小覆盖点,匈牙利算法)
个人心得:二分图啥的一点都不知道,上网借鉴了下,请参考http://blog.csdn.net/thundermrbird/article/details/52231639 加上自己的了解,二分图就是 ...
- 《selenium2 python 自动化测试实战》(7)——定位一组对象
定位一组对象 定位一组对象——find_elements_by_...(),注意,这里是elements,复数.返回的结果是一个列表,我们取值的时候就要用列表取值的方式来获得自己想要的元素.需要注意的 ...
- gradle build scan 插件使用
1. 添加插件 build.gradle plugins { id 'com.gradle.build-scan' version '1.10.2' } buildScan { // Uncommen ...
- js写的体彩足球预测分析
最近室友在玩世界杯的预测,我顺手写点分析:下面直接给出代码,很简单的,拷贝代码直接存为html文件就可以了.---------mq0036 说明下:需要你自己去找个Jquery引用到当前页面就可以了 ...
- Youtube 视频下载
Youtube 视频下载 由于特殊原因,需要下载 Youtube 的视频. https://www.clipconverter.cc/
- Java NIO简单介绍(一)
Java NIO( New IO) 是从Java 1.4版本开始引入的 一个新的IO API,可以替代标准的Java IO API. NIO与原来的IO有同样的作用和目的,但是使用的方式完全不同,NI ...