Description

Background
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

The 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

The 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

Huge input,scanf is recommended.
题意:一共有两种虫子,一个男性,另一个女性,男性只能喜欢女性,同理;问是否存在同性恋;
题解:此题是典型的加权并查集,而加权并查集就是解决同一集合内元素之间的关系,我们用pre[]数组记录下改节点到跟节点之间的距离,若pre[i]==pre[j]),则说明i,j同性恋,反之不是(在i,j是同一个集合(有共同的跟节点)的情况下),偌i,j属于不同的集合,则将两个集合合并,并跟新i,j 根节点之间的距离pre[root(i)]=(abs(str[i]-str[j])+1)%2;
pre[i]==0,表示与根节点同性
pre[i]==1,表示与跟节点异性

 #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(加权并查集)的更多相关文章

  1. A Bug's Life(加权并查集)

    Description Background  Professor Hopper is researching the sexual behavior of a rare species of bug ...

  2. hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14

    题意: 有一个运动场,运动场的坐席是环形的,有1~300共300列座位,每列按有无限个座位计算T_T. 输入: 有多组输入样例,每组样例首行包含两个正整数n, m.分别表示共有n个人,m次操作. 接下 ...

  3. hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

    这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...

  4. HDU 3407.Zjnu Stadium 加权并查集

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. P1196 银河英雄传说(加权并查集)

    P1196 银河英雄传说 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在 ...

  6. Zjnu Stadium(加权并查集)

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. 洛谷 P2024 [NOI2001]食物链(种类并查集,加权并查集)

    传送门 解题思路 加权并查集: 什么是加权并查集? 就是记录着每个节点到它的父亲的信息(权值等). 难点:在路径压缩和合并节点时把本节点到父亲的权值转化为到根节点的权值 怎么转化呢? 每道题都不一样Q ...

  8. UVALive 4487 Exclusive-OR 加权并查集神题

    已知有 x[0-(n-1)],但是不知道具体的值,题目给定的信息 只有 I P V,说明 Xp=V,或者 I P Q V,说明 Xp ^ Xq=v,然后要求回答每个询问,询问的是 某任意的序列值 Xp ...

  9. 牛客网-Beauty of Trees 【加权并查集】

    锟斤拷锟接o拷https://www.nowcoder.com/acm/contest/119/A锟斤拷源锟斤拷牛锟斤拷锟斤拷 锟斤拷目锟斤拷锟斤拷 It锟斤拷s universally acknow ...

随机推荐

  1. 遍历list方法

    [理论] for(String s : list){   System.out.print(s+"/t");  }    for(int i = 0;i<list.size( ...

  2. eclipse 无用代码扫描工具UCDetector

    本文主要介绍UCDetector(无用代码扫描工具)使用方法及局限 对于沉积或多方接手开发的项目经常会遇到一坨一坨无用的代码,但一个一个类查找又相当费时,这里介绍一个eclipse插件扫描没有引用的类 ...

  3. FastAdmin 关于跨域问题解决

    FastAdmin 关于跨域问题解决 之前很久之前收集到社区的问题. https://forum.fastadmin.net/thread/277 今天又有人问到,无法打开,估计是网络问题. 以下为完 ...

  4. Oracle 11gR2 RAC集群服务启动与关闭总结

    引言:这写篇文章的出处是因为我的一名学生最近在公司搭建RAC集群,但对其启动与关闭的顺序和原理不是特别清晰,我在教学工作中也发现了很多学员对RAC知识了解甚少,因此我在这里就把RAC里面涉及到的最常用 ...

  5. 嵌入式无法使用QAudioDeviceInfo类

    修改: 1.修改pro文件 增加    QT += multimedia 2.修改Makefile文件,LIBS = $(SUBLIBS)  -L/opt/qt-4.8.5/lib -lQtMulti ...

  6. 7个去伪存真的JavaScript面试题

    1.创建JavaScript对象的两种方法是什么? 这是一个非常简单的问题,如果你用过JavaScript的话.你至少得知道一种方法.但是,尽管如此,根据我的经验,也有很多自称是JavaScript程 ...

  7. 基于Spring Boot和Spring Cloud实现微服务架构

    官网的技术导读真的描述的很详细,虽然对于我们看英文很费劲,但如果英文不是很差,请选择沉下心去读,你一定能收获好多.我的学习是先从Spring boot开始的,然后接触到微服务架构,当然,这一切最大的启 ...

  8. javascript基础-js对象

    一.js对象的创建 1.普通最简单的方式 var teacher = new Object( ); teacher.name = "zhangsan"; teacher.age = ...

  9. GOF23设计模式之命令模式(command)

    一.命令模式概述 将一个请求封装到一个对象,从而使得可用不同的请求对客户进行参数化. 二.命令模式结构 (1)Command 抽象命令类 (2)ConcreteCommand 具体命令类 (3)Inv ...

  10. java代码-----实现4个人打牌游戏的相关代码。线程

    总结:发送线程Sender().和接收线程Receiver() 虽然,这里的Sender()发送线程和Receiver()都有相同的睡眠时间,但是由于线程调度的不确定性,是的发送线程Sender每次发 ...