HDU 1512 Monkey King(左偏树)
Description
Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).
And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.
Input
First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).
Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.
Output
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ; int n, m;
int fa[MAXN]; int key[MAXN], child[MAXN][], dist[MAXN];
int stk[MAXN], top, node_cnt;
int root[MAXN]; void init() {
dist[] = -;
top = node_cnt = ;
} int newNode(int k) {
int x = top ? stk[top--] : ++node_cnt;
dist[x] = ; key[x] = k;
child[x][] = child[x][] = ;
return x;
} void maintain(int &x) {
if(dist[child[x][]] < dist[child[x][]])
swap(child[x][], child[x][]);
dist[x] = dist[child[x][]] + ;
} int merge(int &x, int &y) {
if(x == ) return y;
if(y == ) return x;
if(key[y] > key[x]) swap(x, y);
child[x][] = merge(child[x][], y);
maintain(x);
return x;
} int del(int &x) {
if(x != ) {
stk[++top] = x;
return merge(child[x][], child[x][]);
}
return ;
} int getfather(int x) {
return fa[x] == x ? x : fa[x] = getfather(fa[x]);
} int merge2(int x, int y) {
return fa[x] = y;
} void solve(int u, int v) {
int fu = getfather(u);
int fv = getfather(v);
if(fu == fv) {
printf("-1\n");
return ;
}
int p1 = newNode(key[root[fu]] / );
int p2 = newNode(key[root[fv]] / );
int p3 = del(root[fu]);
int p4 = del(root[fv]);
p3 = merge(p1, p3);
p4 = merge(p2, p4);
int x = merge2(fu, fv);
root[x] = merge(p3, p4);
printf("%d\n", key[root[x]]);
} int main() {
int k, u, v;
while(scanf("%d", &n) != EOF) {
init();
for(int i = ; i <= n; ++i) {
scanf("%d", &k);
root[i] = newNode(k);
fa[i] = i;
}
scanf("%d", &m);
while(m--) {
scanf("%d%d", &u, &v);
solve(u, v);
}
}
}
使用pb_ds库。使用方法可以参考WC2015的论文《C++的pb_ds库在OI中的应用》。
下面代码使用的Tag为pairing_heap_tag。
此外,binomial_heap_tag为982MS,rc_binomial_heap_tag直接MLE了。
代码(858MS):
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <ext/pb_ds/priority_queue.hpp> using __gnu_pbds::priority_queue; const int MAXN = ; priority_queue<std::pair<int, int>, std::less<std::pair<int, int> >, __gnu_pbds::pairing_heap_tag> root[MAXN];
int fa[MAXN];
int n, m; int get_set(int x) {
return fa[x] == x ? x : fa[x] = get_set(fa[x]);
} int solve(int u, int v) {
int fu = get_set(u), fv = get_set(v);
if(fu == fv) return -;
std::pair<int, int> best_u = root[fu].top(); root[fu].pop();
std::pair<int, int> best_v = root[fv].top(); root[fv].pop();
root[fu].push(std::make_pair(best_u.first / , best_u.second));
root[fv].push(std::make_pair(best_v.first / , best_v.second));
root[fu].join(root[fv]); fa[fv] = fu;
return root[fu].top().first;
} int main() {
int k, u, v;
while(scanf("%d", &n) != EOF) {
for(int i = ; i <= n; ++i) {
scanf("%d", &k);
root[i].clear();
root[i].push(std::make_pair(k, i));
fa[i] = i;
}
scanf("%d", &m);
while(m--) {
scanf("%d%d", &u, &v);
printf("%d\n", solve(u, v));
}
}
}
HDU 1512 Monkey King(左偏树)的更多相关文章
- hdu 1512 Monkey King 左偏树
题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...
- hdu 1512 Monkey King —— 左偏树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...
- HDU 1512 Monkey King (左偏树+并查集)
题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...
- HDU 1512 Monkey King ——左偏树
[题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...
- HDU 1512 Monkey King(左偏堆)
爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...
- 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)只猴子. ...
- 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 : ...
随机推荐
- Eclipse新导入的项目中ctrl+点击指定方法名或类名没有反应,不能跳转问题
项目没有转成java项目 解决方法:右击项目名---选择properties----点击Project Facets,这样就可以实现ctrl+左键点击跳转了. 转成java项目后会报错 解决办法:选中 ...
- 【Django笔记一】windows系统下搭建Django项目
一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 二.创建虚拟环境: 1.为什么要创建虚拟环境: 我们要开发一个新的项目,需要一套独立的Py ...
- python3爬取全站美眉图片
爬取网站:https://www.169tp.com/xingganmeinv 该网站美眉图片有数百页,每页24张,共上万张图片,全部爬取下来 import urllib.request import ...
- lower_case_table_name
linux上是区分表名大小写的,但是可以通过 my.cnf文件中设置不区分! 1.找到my.cnf文件的所在地. find / -name my.cnf 找到这个文件的位置.我服务器上的位置是 /us ...
- IO流,Properties基本功能和遍历
import java.util.Enumeration; import java.util.Iterator; import java.util.Properties; import java.ut ...
- angularjs中控制器之间的通信----$on、$emit和$broadcast解析
$on.$emit和$broadcast使得event.data在controller之间的传递变的简单. $emit只能向parent controller传递event与data $broadca ...
- Hive(6)-DML数据操作
一. 数据导入 1. 语法 load data [local] inpath 'path' [overwrite] into table table_name [partition (partcol1 ...
- 大数据&人工智能&云计算
仅从技术上讲大数据.人工智能都包含工程.算法两方面内容: 一.大数据: 工程: 1)云计算,核心是怎么管理大量的计算机.存储.网络. 2)核心是如何管理数据:代表是分布式存储,HDFS 3)核心是如何 ...
- 报错: Name node is in safe mode
将本地文件拷贝到hdfs上去,结果上错误:Name node is in safe mode 这是因为在分布式文件系统启动的时候,开始的时候会有安全模式,当分布式文件系统处于安全模式的情况下,文件系统 ...
- 观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录 ASP.NET Core MVC入门 Asp.Net Core启动和配置 Program类,Main方法 Startup类 依赖注入,I ...