Description

Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can't avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.
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

There are several test cases, and each case consists of two parts.
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

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.
 
题目大意:一开始有n只孤独的猴子,然后他们要打m次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识o(∩_∩)o )。问每次打完架之后那俩猴子最牛叉的朋友战斗力还有多少,若朋友打架就输出-1.
思路:需要的操作有,选出最牛叉的猴子,合并两堆猴子,让最牛逼的猴子战力减半,比较合适的数据结构就是左偏树啦~
 
代码(781MS):
 #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(左偏树)的更多相关文章

  1. hdu 1512 Monkey King 左偏树

    题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...

  2. hdu 1512 Monkey King —— 左偏树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...

  3. HDU 1512 Monkey King (左偏树+并查集)

    题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...

  4. HDU 1512 Monkey King ——左偏树

    [题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...

  5. HDU 1512 Monkey King(左偏堆)

    爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...

  6. ZOJ2334 Monkey King 左偏树

    ZOJ2334 用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用. 左偏树代码的核心也是两棵树的合并! 代码有些细节需要注意. #include&l ...

  7. zoj 2334 Monkey King/左偏树+并查集

    原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...

  8. HDU1512 ZOJ2334 Monkey King 左偏树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...

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

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

  10. 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 : ...

随机推荐

  1. python3中sys.argv[]小记

    1.python3中sys.argv[]用于传递程序外部的参数,外部一般指命令行输入的参数,argv[]所传递的参数实质上是一个列表,其第一个元素为程序本身. 2. sys.argv[] #传入的参数 ...

  2. mongo配置项说明

    mongo configure 配置文件   storage:     dbPath: mongod实例存储其数据的目录.     indexBuildRetry: 指定是否mongod在下次启动时重 ...

  3. 【sql server常用操作{增删改查}】

    use DB_x   go   drop database DB_y   create database DB_y --创建数据库   on primary --指定主数据文件   (   name= ...

  4. ThinkPHP5.1完全开发手册.CHM离线版下载

    ThinkPHP5.1完全开发手册.CHM离线版下载 ThinkPHP5.1完全开发手册离线版.CHM下载地址 百度云:链接: https://pan.baidu.com/s/1b4jKJN-8UyI ...

  5. 微信支付tp5.1集合

    多商户号微信支付 配置 自己改一改 逻辑 就好了! 写的菜 勿喷 extend下面 主要目录 多商户号 配置项 根据自己的需求更改 可能有一些地方存在BUG 自己调试一下 就OK了,别像一个麻瓜一样 ...

  6. Hadoop-Hive学习笔记(1)

    1. Hive什么 a.Hive是基于Hadoop的一个数据仓库工具(注意不是数据仓库),将结构化的数据文件映射成一张数据库表. b.Hive是SQL的解析引擎,可以把sql语句转换成MapReduc ...

  7. django的Request-7

    目录 1. 从url中获取截取 2. QueryDict (1). QueryDict.get(key, [default]) (2). QueryDict.getlist(key, [default ...

  8. kali aquatone安装

    https://www.jianshu.com/p/418eedb9d9c8

  9. Java设计模式(13)——结构型模式之桥梁模式(Bridge)

    一.概述 概念 将抽象与实现脱耦,使得抽象和实现可以独立运行 UML图 角色: 角色关系 二.实践 按照上面的角色建立相应的类 抽象化角色 /** * 抽象化角色 * * @author Admini ...

  10. MySQL入门第一天——概述、数据表与约束操作

    一.概述 1.安装 初学MySQL,我们下载msi的安装版:http://dev.mysql.com/downloads/file.php?id=457403 安装的过程文字简述可以参考之前随笔:ht ...