hdu1512
题解:
每一次合并两个对
修改操作就和普通的堆一样
代码:
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int val[N],n,m,rt[N],tot,dist[N],c[N][],a[N],x,y,fa[N],size[N];
int merge(int x,int y)
{
if (!x||!y)return x+y;
if (val[x]<val[y])swap(x,y);
c[x][]=merge(c[x][],y);
size[x]=size[c[x][]]+size[c[x][]]+;
fa[c[x][]]=x;
if (dist[c[x][]]<dist[c[x][]])swap(c[x][],c[x][]);
dist[x]=dist[c[x][]]+;
return x;
}
int top(int x)
{
for (;fa[x]!=x;x=fa[x]);
return x;
}
int newnode(int x)
{
val[++tot]=x;
size[tot]=;fa[tot]=tot;
c[tot][]=c[tot][]=dist[tot]=;
return tot;
}
void down(int x)
{
int i=x;
if (c[x][]&&val[i]<val[c[x][]])i=c[x][];
if (c[x][]&&val[i]<val[c[x][]])i=c[x][];
if (x!=i)
{
swap(val[x],val[i]);
down(i);
}
}
int main()
{
while (~scanf("%d",&n))
{
memset(val,,sizeof val);
memset(c,,sizeof c);
memset(rt,,sizeof rt);
memset(fa,,sizeof fa);
tot=;
for (int i=;i<=n;i++)scanf("%d",&a[i]);
for (int i=;i<=n;i++)rt[i]=newnode(a[i]);
scanf("%d",&m);
while (m--)
{
scanf("%d%d",&x,&y);
int tx=top(x),ty=top(y);
if (tx==ty)puts("-1");
else
{
val[tx]/=;down(tx);
val[ty]/=;down(ty);
rt[tx]=merge(tx,ty);rt[ty]=;
printf("%d\n",val[rt[tx]]);
}
}
}
}
hdu1512的更多相关文章
- HDU1512 ZOJ2334 Monkey King 左偏树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...
- hdu1512 Monkey King
Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each ...
- [HDU1512/ZOJ2334]Monkey King-左偏树-可合并堆
Problem Monkey King Solution 本题是裸的左偏树,一个模板就可以过了.对于每个操作对节点先删除/2再合并. 注意本题在HDU上评测特别坑,是多组数据,而且经常出现MLE的情况 ...
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
- [HDU1512]Monkey King(左偏树)
用并查集维护猴子们的关系,强壮值用左偏树维护就行了 Code #include <cstdio> #include <algorithm> #include <cstri ...
- 左偏树自己的一点理解【hdu1512】【Monkey King】
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34693563 向大(hei)佬(e)势力学(di ...
- hdu1512 Monkey King(并查集,左偏堆)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1512 题目大意:有n个猴子,一开始每个猴子只认识自己.每个猴子有一个力量值,力量值越大表示这个猴子打架 ...
- 帝都Day4(3)——还是数据结构
可并堆 左偏树中 dist[x]=dist[rs[x]]+1 合并的时候,把权志较大的根作为根节点,把这棵树右子树和另一棵树合并. 说明白点:(上图描述有点问题) 设x表示根权值较大的左偏树,y表示根 ...
- 左偏树 / 非旋转treap学习笔记
背景 非旋转treap真的好久没有用过了... 左偏树由于之前学的时候没有写学习笔记, 学得也并不牢固. 所以打算写这么一篇学习笔记, 讲讲左偏树和非旋转treap. 左偏树 定义 左偏树(Lefti ...
随机推荐
- P2750 贰五语言Two Five USACO5.5 记忆化搜索
正解:记搜+逼近 解题报告: 神仙题预警,,, 我真滴觉得还是挺难的了,,, 大概说下思路趴QAQ 首先我们要知道逼近法是什么! 逼近法,有点像二分的思路,以这题为例举个eg 假如它给了个数字k.我们 ...
- jQuery.outerWidth() 函数具体解释
outerWidth()函数用于设置或返回当前匹配元素的外宽度.外宽度默认包含元素的内边距(padding).边框(border),但不包含外边距(margin)部分的宽度.你也能够指定參数为true ...
- Spark的Java API例子详解
package com.hand.study; import scala.Tuple2; import org.apache.spark.SparkConf; import org.apache.sp ...
- java:判断二进制数据中第n位是否为1
可以使用位运算来判断. &是位的与运算符,是指二进制数按位“与”的操作, 逻辑与就是两者都为真的时候才为真,其他真假,假真,假假的运算结果都是假.二进制写法如下 1 & 1 = 1 , ...
- python16_day34【设计模式】
一.简单工厂模式 # coding : utf-8 # create by ztypl on 2017/5/24 from abc import abstractmethod, ABCMeta cla ...
- docker——核心实现技术
作为一种容器虚拟化技术,Docker深度应用了操作系统的多项底层支持技术. 早期版本的Docker是基于已经成熟的Linux Container(LXC)技术实现的.自从0.9版本起,Docker逐渐 ...
- SpringCloud配置
encrypt说明 名称 默 认 描述 encrypt.fail-on-error true 标记说,如果存在加密或解密错误,进程将失败. encrypt.key 对称密钥.作为一个更强大的替代方 ...
- fiddler抓包HTTPS配置及代理设置
使用fiddler抓包过程中遇到一系列的问题,浪费了大半天时间~~~写下解决办法 按照网上方法配置之后还是无法抓到cookies提示各种证书错误 1.卸载fiddler重新安装,设置 2.设置步骤 ( ...
- WebStorm下使用TypeScript
TypeScript也可使用Visual Studio 进行开发 TypeScript官网地址:(http://www.typescriptlang.org/) 1.先安装WebStorm WebSt ...
- C++ string和C风格字符串
https://msdn.microsoft.com/en-us/library/syxtdd4f.aspx#basic_string__replace If you need to convert ...