题目描述

在一个森林里住着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. (23)IO之打印流 PrintStream & Printwriter

    PrintStream PrintStream可以接受文件和其他字节输出流,所以打印流是对普通字节输出流的增强,其中定义了很多的重载的print()和println(),方便输出各种类型的数据. Pr ...

  2. laravel框架中所用到的依赖注入

    用Laravel开发前前后后有2个月左右了,之前一直写Java,就像找到Java和PHP之前的共同点,用Java的某些原理去理解PHP会发现还是有很多共通之处的.Java的依赖注入已经是一个很常见的概 ...

  3. MyBatis 源码分析——映射结果

    MyBatis最后一步一定是处理相关的结果——把数据映射成对应的模型对象.事实上在笔者看来如果读者们了解了mybatis如何去执行数据库,又是如何处理数据结果.那么就了解了mybatis的主要路线.因 ...

  4. pyqt样式表语法笔记(中)

    pyqt样式表语法笔记(中) pyqt QSS python 样式表 一.弹窗 在日常的各种桌面软件的使用中,我们都会碰到弹窗.例如注册,登录的时候,会有相应的信息弹窗,这里就以信息收集弹窗为例进行弹 ...

  5. 初步认识Thymeleaf:简单表达式和标签。(二)

    本篇文章是对上篇文章中Thymeleaf标签的补充. 1.th:each:循环,<tr th:each="user,userStat:${users}">,userSt ...

  6. Servlet3.0新特性使用详解

    可插拔的Web框架 几乎所有基于Java的web框架都建立在servlet之上.现今大多数web框架要么通过servlet.要么通过Web.xml插入.利用标注(Annotation)来定义servl ...

  7. SQL 游标的应用

    ----------------SQL游标应用-----------------if object_id('tempdb..#test0001') is not null drop table #te ...

  8. Omi官方插件系列 - omi-transform介绍

    原文链接 - https://github.com/AlloyTeam/omi/tree/master/tutorial 写在前面 Omi框架正式发布了omi-transform作为Omi组件化开发特 ...

  9. Zookeeper的安装和初步使用

    1. Zookeeper集群角色 Zookeeper集群的角色:  Leader 和  follower  (Observer) zk集群最好配成奇数个节点 只要集群中有半数以上节点存活,集群就能提供 ...

  10. 用Xamarin + VS 编写Android程序体验及其与Android Studio的比较

    昨天看了微软2016Build大会,Xamarin免费了.恩,5亿美刀的家伙,哈哈,我也要体验一下..... 1. 首先在Xamarin官网下载安向导:https://www.xamarin.com/ ...