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!

题目意思:t组数据,n个虫子,m组相互喜爱的关系,虫子分为雌雄两种,每个虫子只有一个性别,问是否存在同性恋的虫子。

解题思路:一开始我的思路是希望通过将虫子划分为雌雄两个集合,看看这两个集合中是否出现了环,出现了环则说明存在着同性恋。我看了看网上的题 解,给出了加权并查集的概念,这里我就试着使用这个思想来解题。

加权并查集: 有的时候,不仅需要像普通并查集一样记录一些元素之间有无关系,还需要记录它们之间有怎样的关系,这时候就需要引入加权并查集。 通常情况下,用一个数组r来记录这些关系,r[i]表示元素i与父结点的关系。至于是什么关系,还要根据具体要求来看。 在find(x)函数中进行路径压缩的同时,许多结点的父结点会改变,这时就需要根据实际情况调整权值以保证其正确性。 在union(x,y)函数中,(不妨设将y集合并入x集合)由于y的父结点的改变,需要调整y对应的权值,但不需要调整y的子结点对应的权值,因为子结点 权值会在find(子结点)时得到调整。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int pre[];
int r[];///r=0代表与根节点同性
int Find(int x)
{
int t;
if(pre[x]==x)
{
return x;
}
t=pre[x];
pre[x]=Find(pre[x]);///压缩路径
r[x]=(r[x]+r[t]+)%;
return pre[x];
}
void Union(int a,int b)
{
int x,y;
x=Find(a);
y=Find(b);
pre[x]=y;
r[x]=(r[b]-r[a])%;
}
int main()
{
int t,i,j,k,flag;
int n,m,a,b;
scanf("%d",&t);
for(k=;k<=t;k++)
{
flag=;
scanf("%d%d",&n,&m);
for(i=; i<=n; i++)
{
r[i]=;
pre[i]=i;
}
for(i=; i<=m; i++)
{
scanf("%d%d",&a,&b);
if(Find(a)==Find(b))
{
if(r[a]==r[b])///同性
{
flag=;
}
}
else
{
Union(a,b);
}
}
if(flag)
{
printf("Scenario #%d:\nSuspicious bugs found!\n\n",k);
}
else
{
printf("Scenario #%d:\nNo suspicious bugs found!\n\n",k);
}
}
return ;
}

A Bug's Life(加权并查集)的更多相关文章

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

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

  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. css模型框

    在 CSS 中,width 和 height 指的是内容区域的宽度和高度.增加内边距.边框和外边距不会影响内容区域的尺寸,但是会增加元素框的总尺寸. 假设框的每个边上有 10 个像素的外边距和 5 个 ...

  2. 1010 一元多项式求导(用while接收输入)

    题目: 知识点for me: 该题的输入并非是按回车后数据输入完毕也不是给定数据长度,而是输入到文件末尾.可以有以下三种写法: (1)while(cin>>a)  (2)while(sca ...

  3. linux 学习第十六天(Samba配置)

    Samba 服务 yum install samba mv smb.conf smb.conf.bak cat smb.conf.bak | grep -v "#" | grep ...

  4. scrapy基础

    scrapy Scrapy 是用 Python 实现的一个为了爬取网站数据.提取结构性数据而编写的应用框架. Scrapy 常应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. Scrapy ...

  5. ionic ios 打包 真机测试常见问题

    1.ionic 项目在windows下正常打包安卓包时  迁移到mac下打包ios时  不需要复制平台目录platforms即可  不用再mac下去安装各种插件信息 2.ionic 下不能访问api信 ...

  6. Docker toolbox换源

    一 docker toolbox安装 略.... 阿里云加速器地址 https://jbriwmh3.mirror.aliyuncs.com 二 为docker toolbox更换国内源 docker ...

  7. 基于visual studio 2017 以及cubemx 搭建stm32的开发环境(0)

    (1)安装visual studio 2017 官网下载安装即可 (2)安装visual GDB 链接:https://pan.baidu.com/s/1TgXI1BRQLAWiWlqCcIS9TA ...

  8. go语言的指针

    什么是指针 一个指针变量指向了一个值的内存地址.(也就是我们声明了一个指针之后,可以像变量赋值一样,把一个值的内存地址放入到指针当中.) 类似于变量和常量,在使用指针前你需要声明指针.指针声明格式如下 ...

  9. 数据结构与算法之排序(1)冒泡排序 ——in dart

    最经典的入门排序算法,冒泡排序,dart语言实现.数组仍然采用随机生成的数组,使用dart内置的List 的generate方法,排序前后分别打印出数组,以观察效果. import 'dart:mat ...

  10. Swift里performSelector方法的替代

    最近在回答StackOverflow的问题时,发现performSelector方法在Swift被去掉,Apple的注释是这个方法被去掉是因为不安全: NOTE The performSelector ...