Problem Description
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can't avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.

Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).

And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.

 
Input
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.

 
Output
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.
 
Sample Input
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
 
Sample Output
8
5
5
-1
10

可并堆裸题,没什么思维难度。只要学会了就会打了.

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
const int MANX=;
using namespace std;
int fa[MANX],r[MANX],l[MANX],v[MANX],d[MANX];
int n,m; void cl(){
for(int i=;i<=n;i++) fa[i]=i;
memset(r,,sizeof(r));
memset(l,,sizeof(l));
memset(v,,sizeof(v));
} int find(int x){
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
} int merge(int x,int y){
if(!x) return y;
if(!y) return x;
if(v[x]<v[y]) swap(x,y);
r[x]=merge(r[x],y);fa[r[x]]=x;
if(d[r[x]]>d[l[x]]) swap(l[x],r[x]);
else d[x]=d[r[x]]+;//
return x;
} int del(int x){
int lz=l[x],rz=r[x];
l[x]=r[x]=d[x]=;fa[lz]=lz,fa[rz]=rz;
return merge(lz,rz);
} int main(){
while(scanf("%d",&n)!=EOF){
cl();
for(int i=;i<=n;i++) scanf("%d",&v[i]);
scanf("%d",&m);
for(int i=;i<=m;i++){
int x,y;scanf("%d%d",&x,&y);
int ll=find(x),rr=find(y);
if(ll==rr){printf("-1\n");}
else{
v[ll]/=,v[rr]/=;
int lz=del(ll),rz=del(rr);
lz=merge(ll,lz),rz=merge(rz,rr);
printf("%d\n",v[merge(lz,rz)]);
}
}
}
}

HDU - 1512  Monkey King的更多相关文章

  1. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

  2. hdu 5201 The Monkey King【容斥原理+组合数学】

    原来我一开始以为的\( O(n^2) \)是调和级数\( O(nlog_2n) \)的! 首先枚举猴王的桃子个数\( x \),然后使用容斥原理,枚举有至少\( k \)个不满足的条件,那么这\( k ...

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

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

  4. hdu 1512 Monkey King 左偏树

    题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...

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

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

  6. 1512 Monkey King

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

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

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

  8. ZOJ 2334 Monkey King

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

  9. P1456 Monkey King

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

随机推荐

  1. Spring源码分析之-加载IOC容器

    本文接上一篇文章 SpringIOC 源码,控制反转前的处理(https://mp.weixin.qq.com/s/9RbVP2ZQVx9-vKngqndW1w) 继续进行下面的分析 首先贴出 Spr ...

  2. python3接口测试之webservice接口测试第三方库选择及新手问题

    一.使用python3做webervice接口测试的第三方库选择suds-jurko库,可以直接pip命令直接下载,也可以在pypi官网下载压缩包进行手动安装 二.安装好后,导入Client:from ...

  3. <xsl:apply-templates>和<xsl:call-template>的区别

    <xsl:apply-templates> 应用模板,故名思意,将定义好的模板应用到 XML 的节点上.  可以调用 XML 文档的节点,使 XSL 文档可以渲染 XML 元素内的数据,  ...

  4. ES6-数组的新方法

    1.Array.of() 方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型. Array.of() 和 Array 构造函数之间的区别在于处理整数参数:Array.of(7)创建一个 ...

  5. Termux 高级终端安装使用配置教程

    Termux 高级终端安装使用配置教程,这篇文章拖了有小半年.因为网上相关的文章相对来说还是比较少的,恰好今天又刷了机,所以就特意来总结一下,希望本文可以帮助到其他的小伙伴.发挥Android平台更大 ...

  6. JAVA测试(选择题)

    1.分析如下所示的Java代码,其中this关键字的意思是(  ) Public class Test { Private String name; Public String getName(){ ...

  7. VMware Ubuntu 19最新安装详细过程

    ubuntu可以说是目前最流行的Linux改造版了(如果安卓不算的话),现在的很多服务器都是linux系统的,而自己属于那种前端也搞,后台也搞,对框架搭建也感兴趣,但是很多生产上的框架和工具都是安装在 ...

  8. crypto 的使用方法和说明

    crypto 模块提供了加密功能,包含对 OpenSSL 的哈希.HMAC.加密.解密.签名.以及验证功能的一整套封装.我们这里讲crypto AES算法加密 一.使用步骤 1.引入Crypto 1. ...

  9. charles 禁用缓存

    本文参考:charles 禁用缓存 No caching Settings/无缓存工具的用法 弹窗面板上一句话概括了他的工作原理:通过修改请求和响应头来防止缓存; 无缓存工具 无缓存工具阻止客户端应用 ...

  10. Java线程池的拒绝策略

    一.简介 jdk1.5 版本新增了JUC并发编程包,极大的简化了传统的多线程开发.前面文章中介绍了线程池的使用,链接地址:https://www.cnblogs.com/eric-fang/p/900 ...