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. C# 反射 Reflection Assembly

    本章节带来的是反射,反射反射程序员的快乐. 一.什么叫反射 反射:是.net Framework提供给的一个方面metadata的帮助类,可以获取信息并且使用 反射的有点:动态 反射的缺点:1:稍微麻 ...

  2. vue 创建项目的命令

    1 cmd   创建项目 找到指定目录 vue create test   或   vue ui  (可视化创建)推荐 ---------------------------------------- ...

  3. 前端基础-jQuery的最常用的的方法each、data、

    阅读目录 each inArray get index data 一.jQuery中each方法的应用 jQuery中有个很重要的核心方法each,大部分jQuery方法在内部都会调用each,其主要 ...

  4. laravel5.5源码阅读草稿——路由

    laravel 里的路由是由RouteServiceProvider提供的,其中的boot方法为启动项,调用了父类的boot方法. RouteServiceProvider中的boot方法设置了自己与 ...

  5. Python学习——编程语言介绍

    开发语言 高级语言:基于C/汇编等封装的语言,如Python.Java.C#.PHP.Go.ruby.C++……生成字节码让C/汇编去识别 低级语言:直接让计算机底层能识别成机器码的语言(计算机再将机 ...

  6. 【转】 GATK--原始数据预处理

    1. 对原始下机fastq文件进行过滤和比对(mapping) 对于Illumina下机数据推荐使用bwa进行mapping. Bwa比对步骤大致如下: (1)对参考基因组构建索引: 例子:bwa i ...

  7. 实验四: Android程序设计

    实验四 Android程序设计 1 实验目的及要求 1.安装 Android Stuidio. 2.完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号. ...

  8. 2016-2017-2015329 《Java程序设计》第4周学习总结

    学号 2016-2017-2015329 <Java程序设计>第4周学习总结 教材学习内容总结 面向对象有三大特性:封装.继承.多态 封装 封装是指,一种将抽象性函式接口的实例细节部份包装 ...

  9. [SDOI2011]染色 树链剖分

    LG传送门 我写这道题的题解主要是因为洛谷上的题解要么讲的不清要么代码丑滑稽,导致初学时的我调了很久,所以想发个题解方便后来人. 由于要维护的信息还是具有区间可加性,只需要记录一下每个区间的左右端点颜 ...

  10. 使用github高级搜索

    想瞅瞅github上面有哪些中国开发者最活跃,followers最多.可以按照下面的步骤: 打开github的搜索页面 输入 location:china .点search 然后选择不同的排序方式. ...