题目描述

在一个森林里住着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. Spring-boot中使用@ConditionalOnExpression注解,在特定情况下初始化bean

    想要实现的功能: 我想在配置文件中设置一个开关,enabled,在开关为true的时候才实例化bean,进行相关业务逻辑的操作. 具体实现: 1:要实例化的bean 2. 配置类 代码: 想要实例化的 ...

  2. 如何修改SVN客户端中保存的密码

    本文主要讲述Linux命令行下怎么修改已经保存的客户端密码: 第一步,删除 subvision文件: rm -rf ~/.subvision 第二步:执行任意需要访问服务器的svn命令,会提示是否永久 ...

  3. c++学习笔记之继承篇

    title: c++学习笔记之继承篇 date: 2017-03-26 16:36:33 tags: [c++,继承,public,virtual,private,protected] categor ...

  4. 通过 dhcp-agent 访问 Metadata - 每天5分钟玩转 OpenStack(168)

    OpenStack 默认通过 l3-agent 创建和管理 neutron-ns-metadata-proxy,进而与 nova-metadata-api 通信.但不是所有环境都有 l3-agent, ...

  5. Spring MVC 处理异常的3种方式

    使用Spring MVC开发的博客网站时,遇到了如何处理业务层抛出的异常的问题,查阅到了spring官方博客-spring MVC中异常的处理,以下将会以登录模块为示例. 愚蠢的处理方式 处理异常遵循 ...

  6. 一个web应用的诞生(9)--回到用户

    在开始之前,我们首先根据之前的内容想象一个场景,用户张三在网上浏览,看到了这个轻博客,发现了感兴趣的内容,于是想要为大家分享一下心情,恩?发现需要注册,好,输入用户名,密码,邮箱,并上传头像后,就可以 ...

  7. 如何写一手漂亮的 Vue

    前几日听到一句生猛与激励并存,可怕与尴尬同在,最无奈也无解的话:"90后,你的中年危机已经杀到".这令我很受触动.显然,这有些夸张了,但就目前这日复一日的庸碌下去,眨眼的功夫,那情 ...

  8. es6中的let声明变量与es5中的var声明变量的区别,局部变量与全局变量

    自己通过看typescript官方文档里的let声明,与阮一峰老师翻译的的es6学习文档,总结以下三点 1.var声明可以多次重复声明同一个变量,let不行 2.let变量只在块级作用域里面有效果,v ...

  9. css2.1实现图片添加阴影效果

    盒子里面放了img标签,盒子浮动后,盒子的背景图片(就是阴影图片)会应用图片的宽高. <!DOCTYPE html> <html lang="en"> &l ...

  10. 1.熟悉Java基本类库系列 - 目录

    写这个系列是想让自己多熟悉熟悉Java的基本类库,忘记的时候可以在这里看看之前写过的例子,这样就可以很快的回忆起来如何使用了. 这样就可以很节省时间了. ======= 下面是传送门啦 ======= ...