HDU 1512 Monkey King(左偏树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1512
题意:
有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了,否则的话,这两只都会从它们所认识的猴子中派出一只力量值最大的猴子出来,并且这只猴子的力量值会减半,在打过之后,这两只猴子所在的集体就都认识了。
思路:
这是一道左偏树的模板题。
每个节点有5个值,l为左儿子节点,r为右儿子节点,fa为父亲节点(父亲节点的用法与并查集相似),val为优先级(这道题目就是大根堆),dis是距离值(在两棵树合并的时候使用,根据dis来决定左右子树是否需要对换)。
一开始每只猴子就是一棵树,每次决斗就是将两棵树进行合并。
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = +; int n, m; struct Heap
{
int l,r,fa,val,dis;
}t[maxn]; int finds(int x)
{
return t[x].fa == -? x:t[x].fa = finds(t[x].fa);
} int merge(int x, int y)
{
if(x == ) return y; //如果为0的话,就说明是空子树,根节点当然就是另一节点了
if(y == ) return x;
if(t[y].val>t[x].val) swap(x,y); //始终往右子树进行插入
t[x].r = merge(t[x].r,y);
t[t[x].r].fa = x;
if(t[t[x].l].dis < t[t[x].r].dis) swap(t[x].l,t[x].r); //是否需要左右子树的对换,这样是为了右子树尽量短
if(t[x].r == ) t[x].dis = ; //距离的重新分配
else t[x].dis = t[t[x].r].dis + ;
return x;
} int pop(int &root)
{
int l = t[root].l;
int r = t[root].r;
t[root].l = t[root].r = t[root].dis = ;
t[root].fa = -;
t[l].fa = t[r].fa = -; //删除root根节点
return merge(l,r); //这样一来相当于分裂成了两棵子树,重新进行合并,最后返回值为合并后的根节点
} int push(int x, int y)
{
return merge(x,y);
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
{
t[i].l=t[i].r=t[i].dis=;
t[i].fa=-;
scanf("%d",&t[i].val);
}
scanf("%d",&m);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
int x=finds(a);
int y=finds(b);
if(x!=y)
{
t[x].val/=;
int xx = push( pop(x),x);
t[y].val/=;
int yy = push( pop(y),y);
printf("%d\n",t[merge(xx,yy)].val);
}
else puts("-1");
}
}
return ;
}
HDU 1512 Monkey King(左偏树模板题)的更多相关文章
- hdu 1512 Monkey King 左偏树
题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...
- hdu 1512 Monkey King —— 左偏树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...
- HDU 1512 Monkey King (左偏树+并查集)
题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...
- HDU 1512 Monkey King ——左偏树
[题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...
- HDU 1512 Monkey King(左偏堆)
爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...
- ZOJ2334 Monkey King 左偏树
ZOJ2334 用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用. 左偏树代码的核心也是两棵树的合并! 代码有些细节需要注意. #include&l ...
- zoj 2334 Monkey King/左偏树+并查集
原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...
- HDU1512 ZOJ2334 Monkey King 左偏树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
随机推荐
- AtCoder Beginner Contest 085(ABCD)
A - Already 2018 题目链接:https://abc085.contest.atcoder.jp/tasks/abc085_a Time limit : 2sec / Memory li ...
- HDU 1846 Brave Game (巴什博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1846 十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫<勇敢者的游戏& ...
- hdu1599 find the mincost route
题目链接 floyd找最小环 很好理解 #include<algorithm> #include<iostream> #include<cstdlib> #incl ...
- 关于springMVC 传递 对象参数的问题
1.前端请求必须是 post 2.前端数据data必须做 json字符串处理 JSON.stringify(data) 3. contentType: 'application/json', 4.@ ...
- EditPlus配置GTK
--GCC GTK Compile-- 命令:D:\GCC\MinGW_RP_Green\bin\gcc.exe 参数:$(FileName) -w -o $(FileNameNoExt).exe - ...
- rabbitmq集群安装与配置(故障恢复)
0.首先按照http://www.cnblogs.com/zhjh256/p/5922562.html在至少两个节点安装好(不建议单机,没什么意义) 1.先了解rabbitmq集群架构,http:// ...
- git博客好的例子
01: https://github.com/Gaohaoyang/gaohaoyang.github.io 02: https://gaohaoyang.github.io/2018/06/01/a ...
- Codeforces 581F Zublicanes and Mumocrates - 树形动态规划
It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The ...
- Selenium Webdriver wait for JavaScript JQuery and Angular
Hi all, during the last two weeks I was dealing with the best solution to wait for both JQuery, Angu ...
- noip模拟【ping】
70:很容易想到的是枚举每一个可能的答案来判断是否可行,取最优即可,贪心选择. 100:满足题目条件的这个距离是满足单调性的.如果x不行,那么大于x的距离都不行,二分答案. 学会运用二分,by ws_ ...