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. 设置普通用户输入sudo,免密进入root账户

    满足给开发用户开权限,赋予sudo权限.又不让其输入密码的方式: 方式一: 开始系统内部的wheel用户组, 在/etc/suoers 中编辑配置文件如下: %wheel ALL=(ALL) NOPA ...

  2. mariadb 远程访问报:Host xxx is not allowed to connect to this MariaDb server

    刚开始试的是: 结果报错了,哎,这折腾的. 继续折腾,加个密码试试: 再用Navicat试试,果然成功了.

  3. 从原理层面掌握@InitBinder的使用【享学Spring MVC】

    每篇一句 大魔王张怡宁:女儿,这堆金牌你拿去玩吧,但我的银牌不能给你玩.你要想玩银牌就去找你王浩叔叔吧,他那银牌多 前言 为了讲述好Spring MVC最为复杂的数据绑定这块,我前面可谓是做足了功课, ...

  4. 小程序·云开发的HTTP API调用丨实战

    小程序云开发之httpApi调用. 小程序云开发之httpApi调用(返回"47001处理") 技术栈 采用 nodejs + express 搭建web服务器,采用 axios ...

  5. 多线程——Thread类

    进程(Process):“正在执行的程序”,程序进入内存运行就变成了一个进程.一个进程会产生多个线程. 多线程(Multithread):一个进程中同时存在几个执行体.单线程是按照函数的顺序执行,多线 ...

  6. 关于W3Cschool定义的设计模式-常用的9种设计模式的介绍

    一.设计模式 tip:每种设计模式,其实都是为了更高效的,更方便的解决在面对对象编程中所遇到的问题. 什么是设计模式:     是一套经过反复使用.多人知晓的.经过分类的.代码设计经验的总结   为什 ...

  7. 亮剑.NET第二章

    第二章主要讲解C#中各种让人模糊不清的概念,用法,类型等等. 1.Equals()与==区别 当比较两个值类型时,二者基本一致,当比较引用类型时,==比较的是引用类型的地址是否一致,即两个引用是否指向 ...

  8. elasticsearch document的索引过程分析

    elasticsearch专栏:https://www.cnblogs.com/hello-shf/category/1550315.html 一.预备知识 1.1.索引不可变 看到这篇文章相信大家都 ...

  9. SpringBoot和Hibernate整合

    1.先使用idea创建maven项目(这个就不详细讲了,很简单的操作) 2.创建完maven项目之后添加springboot依赖,pom.xml文件如下: <?xml version=" ...

  10. [Pandas] 01 - A guy based on NumPy

    主要搞明白NumPy“为什么快”. 学习资源 Panda 中文 易百教程 远程登录Jupyter笔记本 效率进化 四步效率优化 NumPy 底层进行了不错的优化. %timeit 对于任意语句,它会自 ...