题目描述

在一个森林里住着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. ISO c++ 14 重点介绍[译]

    原文链接 http://marknelson.us/2014/09/11/highlights-of-iso-c14/ 下面是对你的日常开发有重大影响的C++14新变动,列出了一些示例代码,并讨论何时 ...

  2. Spring中一个类的注入和引用是不一样的

    1.在Spring管理下的bean需要以下面这种方式引入(一种注入方式): private MgrService mgrService; public MgrService getMgrService ...

  3. centos6.5 源码安装 gtk 环境

    解决 No package 'gtk+-2.0′ found问题方法:yum install libgnomeui-devel 执行了上面的,下面的就可以放弃了,yum 大法好 首先 yum 安装下面 ...

  4. Vijos1144小胖守皇宫【树形DP】

    皇宫看守 太平王世子事件后,陆小凤成了皇上特聘的御前一品侍卫.皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状:某些宫殿间可以互相望见.大内保卫森严,三步一岗,五步一哨,每个宫殿都要有人全天候看 ...

  5. JavaWeb之HTTP协议

    一.概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. ...

  6. 手机自动化测试:Appium代码之Logger

    手机自动化测试:Appium代码之Logger   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest推出手机自动化测 ...

  7. JavaScript高级程序设计---学习笔记(三)

    函数表达式 定义函数的方式有两种:一种是函数声明,另一种是函数表达式. 关于函数声明,它的一个重要特征就是函数声明提升,意思是在执行代码之前会先读取函数声明所以可以把函数声明放在调用它的语句后面. 而 ...

  8. python 线程与进程

    线程和进程简介 应用程序和进程以及线程的关系? 一个应用程序里可以有多个进程,一个进程里可以有多个线程 最原始的计算机是如何运行的? CPU是什么?为什么要使用多个CPU? 为什么要使用多线程? 为什 ...

  9. 用ElasticSearch搭建自己的搜索和分析引擎

    作者:robben,腾讯高级工程师 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 导语:互联网产品中的检索功能随处可见.当你的项目规模是百度大搜|商搜或者微信公众号搜索这种体量的时候 ...

  10. android布局中画线的方法

    1.自定义View画线 http://fariytale.iteye.com/blog/1264225 下面介绍几种简单的方法 2.textView和View画直线 <TextView andr ...