题目链接: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. Beam编程系列之Python SDK Quickstart(官网的推荐步骤)

    不多说,直接上干货! https://beam.apache.org/get-started/quickstart-py/ Beam编程系列之Java SDK Quickstart(官网的推荐步骤)

  2. Python高级用法------字典无需提前定义key

    from collections import defaultdict import json def tree(): return defaultdict(tree) categories = tr ...

  3. 【Linux相识相知】任务计划和周期性任务

    在我们的生活中,有的工作是例行的,例如每年一次加薪.每年给女朋友过一次生日.每天上班都要打卡等,有的工作是临时发生的,例如明天朋友要来访,你需要准备午餐等等. 像很多例行的工作,你一旦忙起来就很容易忘 ...

  4. [转]Debugging into .NET Core源代码的两种方式

    本文转自:http://www.cnblogs.com/maxzhang1985/p/6015719.html 阅读目录 一.前言 二.符号服务器 三.项目中添加ASP.NET Core源代码 四.写 ...

  5. .net 金额中文大写 日期转中文

    金额中文大写 #region 中文大写 /// <summary> /// 返回中文数字 ,如壹佰元整 /// </summary> /// <param name=&q ...

  6. .NET Core 部署到CentOS–1.创建项目,简单部署

    开发环境:Windows 10,部署环境:阿里云 CentOS 7.3 1. 创建应用 1) 创建项目, 配置应用生成部署包 2) 配置项目 编辑project.json, 追加环境项, 选项可参考这 ...

  7. MySQL8.0加载文件内容报错: ERROR 1148: The used command is not allowed with this MySQL version

    mysql数据库将文件内容加载到表中报错: mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet LINES TERMINAT ...

  8. with和catch改变作用域链

    总结笔记: with和catch会将对象中标识符的解析添加到作用域链的最前端, 标识符的解析就是with()和catch()括号中的对象. var x = 10, y = 10; with ({x: ...

  9. asp.net后台获取html控件的值

    1.asp.net后台获取前台type=text控件的值 前台:<input name="txtName" class="username" type=& ...

  10. python下的selenium安装

    安装python 打开 Python官网,找到“Download”, 在其下拉菜单中选择自己的平台(Windows/Mac),一般的Linux平台已经自带的Python,所以不需要安装,通过打开“终端 ...