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. 数位DP新识

    简单题:HDU2089    HDU3652  HDU4734   HDU3555  POJ3252  HigoCoder1033(需要前导0,或者用方法4) 总结: 1,dfs(pos,state, ...

  2. 每天一个linux命令(权限):【转载】chgrp命令

    在lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是change group的 ...

  3. 使用不安全代码将 Bitmap 位图转为 WPF 的 ImageSource 以获得高性能和持续小的内存占用

    在 WPF 中将一个现成的 Bitmap 位图转换成 ImageSource 用于显示一个麻烦的事儿,因为 WPF 并没有提供多少可以转过来的方法.不过产生 Bitmap 来源却非常多,比如屏幕截图. ...

  4. windows下gvim中文乱码解决方案

    网罗了一些网上的解决windows下gvim中文乱码的解决方案,都试了一遍,可惜都不能完全解决我的所有问题,最后我综合一下网上的两种方案,得到了最后完全解决我的中文乱码问题的方案,配置很简单,就是把下 ...

  5. Mac OS X系统 HomeBrew的安装和简单使用

    1. 前言 作为linux系统的忠实粉丝,我们都很喜欢 (Debian/Ubuntu)系列的apt包管理系统和(Redhat/Fedora)系列的yum包管理系统. 包括Windows用户都有多种方便 ...

  6. sublime python运行插件

    Tools->New plugin 粘贴下面代码,在插件目录新建文件夹,保存 import sublime, sublime_plugin import os class ExampleComm ...

  7. YUYV&YV12&mtk6763

    stImgInOut.stImgInfo.enImageType = UV_IMAGE_TYPE_YV12; stImgInOut.stImgInfo.as32Pitch[0] = pStreamIm ...

  8. CentOS下编译安装LNMP环境

    一.卸载系统预安装的LAMP软件 rpm -qa|grep httpd rpm -e httpd httpd-tools rpm -qa|grep mysql rpm -e mysql mysql-l ...

  9. 什么是YARN

    YARN的核心组件: 1)ResourceManager,扮演Master角色(和HDFS的nameNode很像)主要用于资源分配:RM有两个子组件,分别是Scheduler(Capacity Sch ...

  10. 每30秒运行一下shell脚本

    cd /usr/local/sbin/     //存放shell脚本 目录. vim guoguosql.sh      //每30秒运行一个php文件.   文件路径为 vim /home/www ...