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. python 输入一个字符,是小写转换为大写,大写转换为小写,其他字符原样输出

    s = input('请输入一个字符:') if 'a' <= s <= 'z': print(chr(ord(s) - 32)) elif 'A' <= s <= 'Z': ...

  2. Java之数据类型

    Java数据类型分为基本数据类型和引用数据类型: 1.基本数据类型一共8种:byte,short,int, long,float,double,boolean和char; 具体可分为三类: ① 整数型 ...

  3. Scala相关笔记

    一.Scala概述以及安装 1.   什么是Scala Scala 是一种多范式的编程语言,其设计的初衷是要集成面向对象编程和函数式编程的各种特性.Scala 运行于 Java 平台(Java 虚拟机 ...

  4. previewImage.js图片预览缩放保存插件

    previewImage.js好用的图片预览缩放保存插件

  5. Flume(2)-拓扑结构与Agent内部原理

    一. 拓扑结构 1. 串行模式 这种模式是将多个flume给顺序连接起来了,从最初的source开始到最终sink传送的目的存储系统.此模式不建议桥接过多的flume数量, flume数量过多不仅会影 ...

  6. Linux中的阻塞机制

    我们知道在字符设备驱动中,应用层调用read.write等系统调用终会调到驱动中对应的接口. 可以当应用层调用read要去读硬件的数据时,硬件的数据未准备好,那我们该怎么做? 一种办法是直接返回并报错 ...

  7. python列表的通用操作

    #'+'和'*'#+可以将两个列表拼接为一个列表my_list = [1,2,3]+[4,5,6]#*可以将列表重复指定的次数my_list = [1,2,3]*5 print(my_list) #创 ...

  8. LFS搭建第一天补充

    1.选择lfs的iso 2.启动虚拟机,进入以下界面直接按回车键 3. 选择下面的时间 4.直接默认本地时间 5.语言格式选择UTF-8 6.默认 7.直接回车,进入系统 8.对hda盘进行分区,我分 ...

  9. 深入浅出MFC学习笔记 第三章 MFC六大关键技术之仿真

    0:MFC类层次结构 1:MFC程序的初始化过程CWinApp::InitApplication()CMyWinApp::InitInstance()CMyFrameWnd::CMyFrameWnd( ...

  10. 20155216 2016-2017-2 《Java程序设计》第七周学习总结

    20155216 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 认识Lambda表达式语法 Lambda表达式不需要也不允许使用throws关键字来声明可能 ...