【luogu P1456 Monkey King】 题解
题目链接:https://www.luogu.org/problemnew/show/P1456
左偏树并查集不加路径压缩吧...
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
struct Left_Tree{
int val, fa, son[2], dis;
}h[maxn<<2];
int n, m;
int Merge(int r1, int r2)
{
if(r1 == 0 || r2 == 0) return r1 + r2;
if(h[r1].val < h[r2].val) swap(r1, r2);
h[r1].son[1] = Merge(h[r1].son[1], r2);
h[h[r1].son[1]].fa = r1;
if(h[h[r1].son[0]].dis < h[h[r1].son[1]].dis) swap(h[r1].son[1], h[r1].son[0]);
if(h[r1].son[1] == 0) h[r1].dis = 0;
else h[r1].dis = h[h[r1].son[1]].dis + 1;
return r1;
}
int destory(int r1)
{
int le = h[r1].son[0], ri = h[r1].son[1];
h[le].fa = 0;h[ri].fa = 0;
int res = Merge(le, ri);
h[r1].son[0] = h[r1].son[1] = 0;
return res;
}
int find(int x)
{
while(h[x].fa)
x = h[x].fa;
return x;
}
int main()
{
while(scanf("%d",&n) != EOF)
{
for(int i = 1; i <= n; i++)
{
h[i].val = h[i].fa = 0;
h[i].son[0] = h[i].son[1] = h[i].dis = 0;
}
for(int i = 1; i <= n; i++)
scanf("%d",&h[i].val);
scanf("%d",&m);
for(int i = 1; i <= m; i++)
{
int opt, x, y;
scanf("%d%d",&x,&y);
int t1 = find(x), t2 = find(y);
if(t1 == t2)
{
printf("-1\n");
continue;
}
h[t1].val /= 2; h[t2].val /= 2;
int left = destory(t1);
int right = destory(t2);
left = Merge(left, t1);
right = Merge(right, t2);
left = Merge(left, right);
printf("%d\n",h[left].val);
}
}
return 0;
}
【luogu P1456 Monkey King】 题解的更多相关文章
- P1456 Monkey King
题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...
- 洛谷P1456 Monkey King
https://www.luogu.org/problemnew/show/1456 #include<cstdio> #include<iostream> #include& ...
- ZOJ 2334 Monkey King
并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子 Monkey King ...
- 数据结构(左偏树):HDU 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- Monkey King(左偏树 可并堆)
我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...
- 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- The Monkey King(hdu5201)
The Monkey King Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
随机推荐
- php二维数组排序方法(array_multisort usort)
一维数组排序可以使用asort.ksort等一些方法进程排序,相对来说比较简单.二维数组的排序怎么实现呢?使用array_multisort和usort可以实现 例如像下面的数组: $users = ...
- 聚焦游戏安全,腾讯云GAME-TECH“空降”上海
游戏行业是DDoS攻击高发行业,占DDoS攻击的六成以上,特别是近年来游戏行业的爆发式增长,游戏行业更成为了黑产.外挂.非法信息的聚集地.安全,已然成为游戏行业当前最大的敌人. 6月29日,腾讯云GA ...
- .net 金额中文大写 日期转中文
金额中文大写 #region 中文大写 /// <summary> /// 返回中文数字 ,如壹佰元整 /// </summary> /// <param name=&q ...
- frp使用总结
笔者所知并成功实现内网穿透的方法: 花生壳 (需要花8块钱,使用花生壳给的二级域名,这里不做介绍) ngrok (免费,但是每次重启服务二级域名会变,付费的$5每月不会变) frp(开源免费,需要有自 ...
- (三)PHP网页架站
目前,Windows下已经有集成的PHP网页架站工具,例如:AppServ.WampServer.这些软件将Apache.PHP.MySQL.phpMyAdmin集成到一起,极大地方便了开发者架站.但 ...
- 键盘按键keyCode大全,js页面快捷键
字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S 83 1 49 B 66 K 75 T 84 2 50 C 67 L 76 U 85 3 ...
- Spring Data JPA简单使用
用Spring Data JPA操作数据库 这份教程教你用Spring Data JPA从关系数据库mysql中存储和提取数据.总结来自https://spring.io/guides/gs/acce ...
- main方法击破
什么是main方法? 是类中的一段代码,可以让程序独立运行. public class HelloWord{ public static void main(String[] args) { for ...
- Cocos2d-js 开发记录:自定义按钮
游戏开发总是有些特殊,一般的预制的UI无法满足要求.其实对于不复杂的功能,与其看文档还不如自己写一个.比如游戏中一个虚拟键盘,其中的按键在按下时会增长,变为原来的两倍高度,在原来高度上方显示按键的字如 ...
- sql:无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_Taiwan_Stroke_CI_AS" 之间的排序规则冲突。
--无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_Taiwan_Stroke_CI_AS" 之间的排序规则冲 ...