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. 搭建Extjs框架(二)

    搭建Extjs 框架 二.编写入口文件 app.js,配置extjs 组件\视图文件路径 并将app.js引入index.html       在app.js中指定一些文件的路径,Extjs页面的起始 ...

  2. BFC的特性及使用场景

    BFC(Block Formatting Context)块级格式化上下文,是Web页面 CSS 视觉渲染的一部分,用于决定块盒子的布局及浮动相互影响范围的一个区域. BFC的特性: 1. 属于同一个 ...

  3. 爬虫——Scrapy框架案例二:阳光问政平台

    阳光热线问政平台 URL地址:http://wz.sun0769.com/index.php/question/questionType?type=4&page= 爬取字段:帖子的编号.投诉类 ...

  4. 【一】调通单机版的thrift-python版本

    开发步骤说明 [任务1]调通单机版的thrift-python版本 [任务1]调通单机版的thrift-python版本 安装thrift 创建thrift模块文件并编译 开发python版的clie ...

  5. Matlab R2018a版离线使用帮助文档方法

    转载自:Matlab R2018a版离线使用帮助文档方法 问题 Matlab R2018a版本安装后,帮助文档默认为在线方式,需要使用账号登录,如果没有激活密钥或许可证编号,就无法使用帮助文档了. 方 ...

  6. 【转】odoo装饰器:model

    model装饰器的作用是返回一个集合列表,一般用来定义自动化动作里面,该方法无ids传入. 应用举例: 定义columns langs = fields.Selection(string=" ...

  7. Please ensure JDK installation is valid and compatible with the current OS

    报错如下: Gradle sync failed: Could not run JVM from the selected JDK. Please ensure JDK installation is ...

  8. 【费元星】crt 无法上传文件,总是显示盾牌表示-完美解决

    将如下内容保存到文件中,已.bat 结尾 taskkill /f /im explorer.exeattrib -s -r -h "%userprofile%\AppData\Local\i ...

  9. Ruby 基础教程1-9

    异常 1.异常结构      [ begin]          ...     rescue         [retry]          ...     [ensure]          . ...

  10. InnoDB锁冲突案例演示(续)

      Preface       I've demontstrated several InnoDB locking cases in my previous blog.I'm gonna do the ...