【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 ...
随机推荐
- ListView与ArrayAdapter的搭配使用
在android中,ListView是一种很重要的控件,一般的使用中,常建立一个所需类型的ArrayList,再通过ArrayAdapter把ListView绑定到ArrayList上,通过Array ...
- git命令(转载学习)
Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是 ...
- Django 入门项目案例开发(上)
关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. Django 入门案例开发(中) http://www.cnblogs.com/focusBI ...
- java里面的package/import 和PHP里面的namespace/use 是一模一样的吗
java里面的package/import 和PHP里面的namespace/use 是一模一样的吗? java: php package mypage; namespace mypage; impo ...
- SpringBoot | 第二十九章:Dubbo的集成和使用
前言 今年年初时,阿里巴巴开源的高性能服务框架dubbo又开始了新一轮的更新,还加入了Apache孵化器.原先项目使用了spring cloud之后,已经比较少用dubbo.目前又抽调回原来的行业应用 ...
- [转]什么?你还不会写JQuery 插件
本文转自:http://www.cnblogs.com/joey0210/p/3408349.html 前言 如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jque ...
- js之cookie操作
1.使用document.cookie保存信息 document.cookie = "Test=cooo";alert(document.cookie); //hours为空字符串 ...
- vscode插件推荐
在扩展(Ctrl+Shift+X)中直接搜索这些插件的名字安装即可 1.HTML Snippets 超级使用且初级的H5代码片段以及提示 2.HTML CSS Support 让HTML标签上写cla ...
- 那些你常用的JSP知识
脚本程序 <> 或者,您也可以编写与其等价的XML语句,就像下面这样: <jsp:scriptlet> 代码片段 </jsp:scriptlet>任何文本.HTML ...
- 【Linux】安装配置Tomcat7
第一步:下载Tomcat安装包 下载地址:https://tomcat.apache.org/download-70.cgi [root@localhost ~]# wget http://mirro ...