Monkey King(左偏树)
每次给出要争吵的猴子a和b,用并查集判断如果他们是朋友输出-1
如果不是,找出a,b在的堆的根A,B,分别合并A,B的左右孩子,再合并一下。
之后把A,B的数据更改一下:权值除以2,左右孩子设为0,再插入到堆中即可。
最后输出堆顶。
#include <iostream>
#include <cstdio> using namespace std; int n, m;
int f[], w[], d[], l[], r[]; inline int merge(int x, int y)
{
if(!x || !y) return x + y;
if(w[x] < w[y]) swap(x, y);
r[x] = merge(r[x], y);
f[r[x]] = x;
if(d[l[x]] < d[r[x]]) swap(l[x], r[x]);
d[x] = d[r[x]] + ;
return x;
} inline int find(int x)
{
return x == f[x] ? x : f[x] = find(f[x]);
} inline int pop(int x)
{
int lc = l[x], rc = r[x];
f[lc] = lc;
f[rc] = rc;
l[x] = r[x] = d[x] = ;
return merge(lc, rc);
} int main()
{
int i, j, x, y, fx, fy, x1, y1;
while(~scanf("%d", &n))
{
for(i = ; i <= n; i++)
{
scanf("%d", &w[i]);
f[i] = i;
l[i] = ;
r[i] = ;
d[i] = ;
}
scanf("%d", &m);
for(i = ; i <= m; i++)
{
scanf("%d %d", &x, &y);
fx = find(x);
fy = find(y);
if(fx == fy)
{
printf("-1\n");
continue;
}
x1 = pop(fx);
w[fx] /= ;
x1 = merge(x1, fx);
y1 = pop(fy);
w[fy] /= ;
y1 = merge(y1, fy);
printf("%d\n", w[merge(x1, y1)]);
}
}
return ;
}
Monkey King(左偏树)的更多相关文章
- hdu 1512 Monkey King 左偏树
题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...
- ZOJ2334 Monkey King 左偏树
ZOJ2334 用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用. 左偏树代码的核心也是两棵树的合并! 代码有些细节需要注意. #include&l ...
- zoj 2334 Monkey King/左偏树+并查集
原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...
- HDU1512 ZOJ2334 Monkey King 左偏树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...
- HDU 1512 Monkey King (左偏树+并查集)
题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...
- hdu 1512 Monkey King —— 左偏树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
- LuoguP1456 Monkey King (左偏树)
struct LeftTree{ int l,r,val,dis; }t[N]; int fa[N]; inline int Find(int x){ return x == fa[x] ? x : ...
- HDU 1512 Monkey King ——左偏树
[题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...
- HDU 1512 Monkey King(左偏堆)
爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...
随机推荐
- servlet生命周期:
Servlet生命周期分为三个阶段: 1,初始化阶段 servlet实例创建时调用init()方法,在Servlet的整个生命周期内,init()方法只被调用一次. 2,响应客户请求阶段 调用ser ...
- Spring-bean(零)
内容提要:红为1,黄2,绿3 -----配置形式:基于xml文件的方式:基于注解的方式 -----Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),Facto ...
- hihocoder offer收割编程练习赛9 B 水陆距离
思路: 宽搜,多个起点. 实现: #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- 必看的dockerfile禁忌与建议!
直接上对照组(看第三个run) test1 FROM centos MAINTAINER ** RUN yum -y update RUN yum -y install wget RUN wg ...
- php配置之include_path
在php.ini中配置include_path,可在引入文件时直接引入配置目录下的文件. 项目中就可以直接 引入/var/www/phpxwlib及/var/www/huicuiserver/libs ...
- Win10 系统安装Sql Server2008 R2 数据库遇到的问题及解决办法总结!
1.开始安装时,提示要先安装 “.NET Framework 3.5(包括.NET 2.0和3.0)”,之前已经下载好.NET Framework 3.5 sp1,安装时还是提示要先安装 “.NET ...
- 介绍三款大前端UI框架
一.蚂蚁金服团队推出的基于React antd (全名:ant.design) 友情跳链:https://ant.design/index-cn:使用antd模板:https://pro.ant.de ...
- subprocess使用小方法
import subprocess def create_process(cmd): p = subprocess.Popen(cmd, shell=True, stdout=subp ...
- how to make a function from using global var to not using it
let say, we want to find the bottom left node in a tree.one way to do it is using global vars: /** * ...
- qobject_cast
void QLadderDiagramItem::GetMainForm(DoType sourceType){ for each (QWidget *w in qApp->topLevelWi ...