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. mybatis传单个参数,和<if>标签同时使用的问题

    // Mapper.java EmerEvent selectByAlarmId(Integer alarmId); // Mapper.xml <select id="selectB ...

  2. Linux下Git远程仓库的使用详解

    Git远程仓库Github 提示:Github网站作为远程代码仓库时的操作和本地代码仓库一样的,只是仓库位置不同而已! 准备Git源代码仓库 https://github.com/ 准备经理的文件 D ...

  3. linux系统基础之---文件系统与日志(基于centos7.4 1708)

  4. python装饰器内获取函数有用信息方法

    装饰器内获取函数有用信息方法 .__doc__用于得到函数注释信息 .__name_用于得到函数名 在函数引用装饰器的时候,函数名会变为装饰器内部执行该函数的名字,所有在直接执行函数名加.__doc_ ...

  5. JQuery制作网页—— 第七章 jQuery中的事件与动画

    1. jQuery中的事件: ●和WinForm一样,在网页中的交互也是需要事件来实现的,例如tab切换效果,可以通过鼠标单击事件来实现 ●jQuery事件是对JavaScript事件的封装,常用事件 ...

  6. UIPickerView的简单使用

    UIPickerView是一个选择器它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活,使用也比较简单.下面做了一个关于天气预报的小Demo 用 UI ...

  7. 第3章 Hadoop 2.x分布式集群搭建

    目录 3.1 配置各节点SSH无密钥登录 1.将各节点的秘钥加入到同一个授权文件中 2.拷贝授权文件到各个节点 3.测试无秘钥登录 3.2 搭建Hadoop集群 1.上传Hadoop并解压 2.配置H ...

  8. C语言经典程序100例

    -------------------------------------------------------------------------------- [程序1] 题目:古典问题:有一对兔子 ...

  9. swig与python

    当你觉得python慢的时候,当你的c/c++代码难以用在python上的时候,你可能会注意这篇文章.swig是一个可以把c/c++代码封装为python库的工具.(本文封装为python3的库) 文 ...

  10. PHP.49-TP框架商城应用实例-前台1-公共布局、制作首页

    公共布局包括 1.页头.页脚{layout} 正文:{__COMMENT__} 导入:<layout name="layout"> 2.商品导航{nav.html} = ...