题目链接:HDU - 1512

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.
 
题意描述:有n只猴子,每只猴子有一个值,两只猴子如果打架的话,他们的值各自掉一半。给出m个事件,每个事件给出两只猴子,如果两只猴子不认识的话,打完架就成为了朋友(两只猴子的各自朋友也都互相成为了朋友),求出打完架后两只猴子的所有朋友中值最大的。
算法分析:这道题我刚开始做的时候,想到了并查集,以为这就够了(其实时间复杂度我也不敢直视),交上去果断TLE了,后来看了讨论里有说,尼玛,这就是传说中的左偏树的题目啊,果断得好好学习一下,弥补一下自己的数据结构的知识。
说明:头一次搞左偏树,代码是借鉴别人的,不过真心写的比较好就拿来了。同时,推荐一下集训队的论文,基本上看了论文后就对左偏树有了一定的了解了。

左偏树的特点及其应用

 /*左偏树*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn = +; int father[maxn];
struct node
{
int l,r;
int dis;
int strong;
}LTree[maxn];
int Find(int x)
{
if (father[x]==x) return x;
return father[x]=Find(father[x]);
}
int Merge(int x,int y)
{ //返回合并后的根
if (x==) return y;
if (y==) return x;
if (LTree[x].strong < LTree[y].strong) //大顶堆
swap(x,y);
LTree[x].r = Merge(LTree[x].r,y); //递归合并右子树和Y
int l = LTree[x].l , r = LTree[x].r;
father[r] = x; //更新T右子树的根
if (LTree[l].dis < LTree[r].dis) //维护堆性质
swap(LTree[x].l,LTree[x].r);
if (LTree[x].r == ) //如果没有右子树 则距离为0
LTree[x].dis = ;
else
LTree[x].dis = LTree[LTree[x].r].dis + ;
return x;
}
int del(int x)
{ //返回删除根以后左右子树的合并的根
int l,r;
l=LTree[x].l;
r=LTree[x].r;
father[l]=l;
father[r]=r;
LTree[x].l=LTree[x].r=LTree[x].dis=;
return Merge(l,r);
}
void solve(int x,int y)
{
LTree[x].strong /= ;
LTree[y].strong /= ;
//问每次PK以后,当前这个群体里力量最大的猴子的力量是多少。
int left,right;
left = del(x);
right = del(y);
left = Merge(left,x);
right = Merge(right,y);
left = Merge(left,right);
printf("%d\n",LTree[left].strong);
}
int main()
{
int n,m,x,y;
while (scanf("%d",&n)!=EOF)
{
for (int i= ;i<=n ;i++)
{
scanf("%d",&LTree[i].strong);
LTree[i].l=;
LTree[i].r=;
LTree[i].dis=;
father[i]=i; //起始已自己为父亲
}
scanf("%d",&m);
for (int i= ;i<=m ;i++)
{
scanf("%d%d",&x,&y);
int fx=Find(x),fy=Find(y);
if (fx == fy) printf("-1\n");
else solve(fx,fy);
}
}
return ;
}

hdu 1512 Monkey King 左偏树的更多相关文章

  1. hdu 1512 Monkey King —— 左偏树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...

  2. HDU 1512 Monkey King (左偏树+并查集)

    题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...

  3. HDU 1512 Monkey King ——左偏树

    [题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...

  4. HDU 1512 Monkey King(左偏堆)

    爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...

  5. ZOJ2334 Monkey King 左偏树

    ZOJ2334 用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用. 左偏树代码的核心也是两棵树的合并! 代码有些细节需要注意. #include&l ...

  6. zoj 2334 Monkey King/左偏树+并查集

    原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...

  7. HDU1512 ZOJ2334 Monkey King 左偏树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...

  8. hdu1512 Monkey King(左偏树 + 并查集)

    Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...

  9. LuoguP1456 Monkey King (左偏树)

    struct LeftTree{ int l,r,val,dis; }t[N]; int fa[N]; inline int Find(int x){ return x == fa[x] ? x : ...

随机推荐

  1. cookie换肤功能

    <div class="selectSkin"> <input id="red" class="themeBtn" typ ...

  2. jQuery遍历 filter()方法

    实例 改变所有 div 的颜色,然后向类名为 "middle" 的类添加边框: $("div").css("background", &qu ...

  3. MySQL数据库有哪些特点

    MySQL数据库的特点有: 它是C和C++语言编写的.支持多个操作系统.支持多线程.为多种编程语言提供API.优化SQL算法提高了查询速度以及提供用于管理和检查数据库的管理工具 MySQL数据库 My ...

  4. unity射线碰撞检测+LayerMask的使用

    射线在unity中是个很方便的东西,对对象查找.多用于碰撞检测(如:子弹飞行是否击中目标).角色移动等提供了很大的帮助,在此做个总结与大家分享下 ,若有不足欢迎吐槽 好了,话补多说啦,直接进入主题: ...

  5. 如何出发匿名映射呀【log】

    malloc-9711 [002] .... 40794.642938: mm_vmscan_lru_shrink_inactive: nid=0 zid=1 nr_scanned=3 nr_recl ...

  6. 【bzoj4636】蒟蒻的数列 离散化+线段树

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...

  7. Codeforces Round #392(div 2) 758D (贪心)

    orz 最近被水题卡+FST,各种掉rating 题目大意 一个数s它是n进制的,但是每一位不是用'A','B'....表示的,而是用10,11等等表示的,将它还原成十进制 这种表示方法显然会产生多解 ...

  8. [洛谷P3810]【模板】三维偏序(陌上花开)

    题目大意:有$n$个元素,第$i$个元素有三个属性$a_i,b_i,c_i$,设$f(i)=\sum\limits_{i\not = j}[a_j\leqslant a_i,b_j\leqslant ...

  9. 树上莫队 SPOJ COT2

    题意: 给一棵树,每次查询u到v路径上有多少不同的点权 首先需要证明这类题目符合区间加减性质 摘选一段vfk大牛的证明 用S(v, u)代表 v到u的路径上的结点的集合. 用root来代表根结点,用l ...

  10. 论文笔记《Hand Gesture Recognition with 3D Convolutional Neural Networks》

    一.概述 Nvidia提出的一种基于3DCNN的动态手势识别的方法,主要亮点是提出了一个novel的data augmentation的方法,以及LRN和HRn两个CNN网络结合的方式. 3D的CNN ...