【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 ...
随机推荐
- 基于setTimeout制作滚动广告板
很多网站在其门户页面的上方正中央都会放置一个滚动广告板,用于显示一些推荐信息,用户点击即可进入浏览.比较常见的就是各个公司的官网,电商网站的首页等. 下面是天猫的滚动广告板截图. 其实,不需要借助于什 ...
- java NIO之SelectedKey
SelectedKey是channel与Selector绑定的标记,每将一个channel注册到一个selector就会产生一个SelectedKey,并将这个SelectedKey放入到Select ...
- Windows 下推荐软件
神器 Dism++ Quicker(效率工具) Bandizip 火绒安全软件 Everyting(搜索神器并支持http远程连接) Xmanager VMware Workstation IDMan ...
- [转]微信小程序登录逻辑梳理
本文转自:http://www.jianshu.com/p/d9996cafdb31 官方文档 文档相关地址: 用户登录 获取用户数据 用户数据的签名验证和加解密 ...
- indexOf.substr,substring,charAt的区别
var a = "asdfghjkl" alert(a.substr(1, 3)); // 从下标为1开始,往右数3个长度的数, 显示 sdf; alert(a.s ...
- SpringSecurity 3.2入门(9)自定义权限控制代码实现
1. 一个自定义的filter,必须包含authenticationManager,accessDecisionManager,securityMetadataSource三个属性,我们的所有控制将在 ...
- 4.net两种交互模式
.net两种交互模式 (1) C/S:客户端(Client)/服务器模式(Server) (2) B/S:浏览器(Browser)/服务器模式(Server) 来自为知笔记(Wiz)
- typeScript入门(四)泛型
泛型:软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性. 组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型,这在创建大型系统时为你提供了十分灵活的功能. 在像C#和Ja ...
- Dubbo与Zookeeper、Spring整合使用 maven+springmvc+dubbo+zookeeper
为什么要用dubbo? 还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm http://dubbo.io/ 一般 nginx+tomcat | - ...
- ViewPager+handler实现轮播
先说下功能吧,随便设置一个网络图片链接的集合,作为图片的资源.我用ImageLoader来加载图片,在图片未加载完成时会ProgressBar加载状态 在Handler发送一个空消息来实现页面的轮播 ...