题目链接: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】 题解的更多相关文章

  1. P1456 Monkey King

    题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...

  2. 洛谷P1456 Monkey King

    https://www.luogu.org/problemnew/show/1456 #include<cstdio> #include<iostream> #include& ...

  3. ZOJ 2334 Monkey King

    并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子                       Monkey King ...

  4. 数据结构(左偏树):HDU 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. HDU - 5201 :The Monkey King (组合数 & 容斥)

    As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...

  6. Monkey King(左偏树 可并堆)

    我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...

  7. 1512 Monkey King

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. The Monkey King(hdu5201)

    The Monkey King Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

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

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

随机推荐

  1. 把数据库内容显示在listview上

    数据库操作很简单,但用户想看见的是数据库里的内容,那么让数据库内容显示在屏幕上呢,下面做个简单演示,百变不离其中,先看步骤: 把数据库的数据显示至屏幕1. 任意插入一些数据 定义Javabean:Pe ...

  2. opensuse 安装oracle 界面乱码

    执行.runInstall时,出现界面乱码 export LANG=en_US export LC_ALL=en_US 终端里 执行这两句.用英文界面安装 再执行 .runInstall

  3. 周记1——WebSocket入门

    一周复一周,时间过得飞快,每个周末都是很开心却又很彷徨.开心的是不用工作,彷徨的是自己这周学到了什么.自身的技能有没有提高.如何应对这个日新月异的社会... 本周的工作的开发IM(即时聊天)模块,要用 ...

  4. windows使用bat文件定时备份文件

    遇到一个需求,需要备份Access数据库,Access生成的数据都保存在xx.mdb文件中,所以考虑使用windows任务 定时执行一个备份文件的bat文件来解决这个问题. backup.bat文件代 ...

  5. SqlServer系统表认识和操作

    地址:http://technet.microsoft.com/zh-cn/library/ms178551.aspx 一.sys.sysindexkeys 包含有关数据库的索引中的键或列的信息.(表 ...

  6. AngularJS-自定义过滤器 ng-repeat 求和

    <!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8& ...

  7. 如何用 windows+github搭建一个优美的hexo博客

    1.Hexo简单介绍 Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页. 风一般的速度Hexo基于Nod ...

  8. 进程和程序(Process and Program)

    原出处:http://oss.org.cn/kernel-book/ch04/4.1.htm ----------------------------------个人理解分割线------------ ...

  9. 2.浅析Hadoop之YARN

    YARN也是主从架构,主节点是ResourceManager,从节点是NodeManager,是一种资源分配及任务管理的组件 针对每个任务还有ApplicationMaster应用管理者和Contai ...

  10. angular-自定义模块

    <!DOCTYPE html><html lang="en" ng-app="app"><head> <script ...