题目描述

在一个森林里住着N(N<=10000)只猴子。在一开始,他们是互不认识的。但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识(认识具有传递性)的两只猴子之间。争斗时,两只猴子都会请出他认识的猴子里最强壮的一只(有可能是他自己)进行争斗。争斗后,这两只猴子就互相认识。
  每个猴子有一个强壮值,但是被请出来的那两只猴子进行争斗后,他们的强壮值都会减半(例如10会减为5,5会减为2)。
 现给出每个猴子的初始强壮值,给出M次争斗,如果争斗的两只猴子不认识,那么输出争斗后两只猴子的认识的猴子里最强壮的猴子的强壮值,否则输出 -1。

输入

There are several test cases, and each case consists of two parts.

First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).

Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.

输出

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.

样例输入

5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5

样例输出

8
5
5
-1
10
 
删除就是把左子树合并到右子树,然后再把根节点除以2加入右子树中
 
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
const int N=;
int n;int fa[N];
struct node{
int x,dis;
node *l,*r;
int ldis(){return l?l->dis:;}
int rdis(){return r?r->dis:;}
}a[N];
int gi()
{
int str=;char ch=getchar();
while(ch>'' || ch<'')ch=getchar();
while(ch>='' && ch<='')str=str*+ch-'',ch=getchar();
return str;
}
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
node *root[N],*pos=a;
void newnode(node *&p,int key)
{
p=pos++;
p->l=p->r=NULL;
p->dis=;p->x=key;
}
node *merge(node *p,node *q)
{
if(!p || !q)return p?p:q;
if(p->x<q->x)swap(p,q);
p->r=merge(p->r,q);
if(p->ldis()<p->rdis())swap(p->l,p->r);
p->dis=p->rdis()+;
return p;
}
void Delet(int t)
{
node *R=root[t]->r;
node *L=root[t]->l;
node *rt=root[t];
rt->dis=;rt->l=rt->r=NULL;rt->x>>=;
root[t]=merge(R,L);
root[t]=merge(root[t],rt);
}
void work()
{
int x;
for(int i=;i<=n;i++)
{
fa[i]=i;x=gi();
newnode(root[i],x);
}
int m=gi(),y;
while(m--)
{
x=gi();y=gi();
if(find(x)==find(y)){
printf("-1\n");
continue;
}
else{
int xx=find(x),yy=find(y);
Delet(xx);Delet(yy);
fa[yy]=xx;
root[xx]=merge(root[xx],root[yy]);
printf("%d\n",root[xx]->x);
}
}
return ;
}
int main()
{
while(~scanf("%d",&n)){
work();
pos=a;
}
return ;
}

[Monkey King]的更多相关文章

  1. ZOJ 2334 Monkey King

    并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子                       Monkey King ...

  2. 数据结构(左偏树):HDU 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  3. P1456 Monkey King

    题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...

  4. HDU - 5201 :The Monkey King (组合数 & 容斥)

    As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...

  5. Monkey King(左偏树 可并堆)

    我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...

  6. 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. The Monkey King(hdu5201)

    The Monkey King Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  8. hdu1512 Monkey King

    Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each ...

  9. hdu1512 Monkey King(左偏树 + 并查集)

    Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...

随机推荐

  1. 【方法】Oracle用户密码含特殊字符时的登陆问题

    [方法]Oracle用户密码含特殊字符时的登陆问题 1.1  BLOG文档结构图 1.2  前言部分 1.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它 ...

  2. Java数据类型及其转换&&经常用到的快捷键

    数据类型 基本数据类型分类 (8种) byte .short. int. long. char. float. double .boolean 1个字节占8位   整数型byte 1字节 -128~1 ...

  3. Spark结构式流编程指南

    Spark结构式流编程指南 概览 Structured Streaming 是一个可拓展,容错的,基于Spark SQL执行引擎的流处理引擎.使用小量的静态数据模拟流处理.伴随流数据的到来,Spark ...

  4. 实现类似MVC ViewBag类型的对象

    public class ViewBag : DynamicObject { private readonly Dictionary<string,dynamic> dic=new Dic ...

  5. zookeeper + dubbo + spring boot

    最近开始接触了分布式的一些东西,这里给自己作一个学习笔记. 这里只是做一个运行demo,具体的理论知识就不在这里阐述了. 1.zookeeper的安装与启动 下载地址:http://www.apach ...

  6. UT源码-124

    (1)设计三角形问题的程序 输入三个整数a.b.c,分别作为三角形的三条边,现通过程序判断由三条边构成的三角形的类型为等边三角形.等腰三角形.一般三角形(特殊的还有直角三角形),以及不构成三角形.(等 ...

  7. 使用arcpy.mapping模块批量出图

      出图是项目里常见的任务,有的项目甚至会要上百张图片,所以批量出土工具很有必要.arcpy.mapping就是ArcGIS里的出图模块,能快速完成一个出图工具. arcpy.mapping模块里常用 ...

  8. css中的inline-block

    div { display: inline-block; *display: inline; *zoom: 1; } Basic Support包含值:none | inline | block | ...

  9. 手机自动化测试:appium源码分析之bootstrap八

    手机自动化测试:appium源码分析之bootstrap八   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣, ...

  10. 老李推荐: 第1章1节《MonkeyRunner源码剖析》概述:前言

    老李推荐: 第1章1节<MonkeyRunner源码剖析>概述:前言   前言 相信大家做过安卓移动平台UI自动化开发的必然会用过,至少听过MonkeyRunner这个名字.MonkeyR ...