zoj 2334 Monkey King/左偏树+并查集
原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389
大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值)
两只不认识的猴子之间发生冲突,两只猴子会分别请出它们认识的最强壮的
猴子进行决斗。决斗之后这,两群猴子都相互认识了。
决斗的那两只猴子战斗力减半。。。有m组询问
输入a b表示猴子a和b发生了冲突,若a,b属于同一个集合输出-1
否则输出决斗之后这群猴子(已合并)中最强的战斗力值。。。
具体思路:用并查集判断是否属于同一集合,用左偏树维护一群猴子的战斗力值。
加了垃圾回收,否则容易爆内存。。。
具体如下:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
const int Max_N = ;
struct UnionFind{
int par[Max_N], rank[Max_N];
inline void init(int n){
for (int i = ; i <= n; i++){
par[i] = i;
rank[i] = ;
}
}
inline int find(int x){
while (x != par[x]){
x = par[x] = par[par[x]];
}
return x;
}
inline void unite(int x, int y){
x = find(x), y = find(y);
if (x == y) return;
if (rank[x] < rank[y]){
par[x] = y;
} else {
par[y] = x;
if (rank[x] == rank[y]) rank[x]++;
}
}
};
struct Node{
int v, npl;
Node *ch[];
inline void set(int _v = , int _npl = -, Node *p = NULL){
v = _v, npl = _npl;
ch[] = ch[] = p;
}
inline void push_up(){
npl = ch[]->npl + ;
}
};
struct LeftistTree{
int N, top;
UnionFind rec;
Node *tail, *null;
Node stack[Max_N], *ptr[Max_N], *store[Max_N];
void init(int n){
tail = &stack[];
null = tail++;
null->set();
N = n, top = , rec.init(n);
}
inline Node *newNode(int v){
Node *p = null;
if (!top) p = tail++;
else p = store[--top];
p->set(v, , null);
return p;
}
inline Node* Merge(Node* &x, Node* &y){
if (x == null) return y;
if (y == null) return x;
if (y->v > x->v) std::swap(x, y);
x->ch[] = Merge(x->ch[], y);
if (x->ch[]->npl > x->ch[]->npl)
std::swap(x->ch[], x->ch[]);
x->push_up();
return x;
}
inline int get_max(int i){
return ptr[i]->v;
}
inline void insert(){
int v;
for (int i = ; i <= N; i++){
scanf("%d", &v);
ptr[i] = newNode(v);
}
}
inline void del(int i){
int ret = get_max(i);
Node *x = newNode(ret >> );
store[top++] = ptr[i];
ptr[i] = Merge(ptr[i]->ch[], ptr[i]->ch[]);
ptr[i] = Merge(ptr[i], x);
}
inline void gogo(int a, int b){
int ans = ;
a = rec.find(a), b = rec.find(b);
if (a == b){
printf("-1\n");
return;
}
rec.unite(a, b);
del(a), del(b);
if (rec.rank[a] > rec.rank[b]){
ptr[a] = Merge(ptr[a], ptr[b]);
ans = ptr[a]->v;
} else {
ptr[b] = Merge(ptr[a], ptr[b]);
ans = ptr[b]->v;
}
printf("%d\n", ans);
}
}lft;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m, a, b;
while (~scanf("%d", &n)){
lft.init(n), lft.insert();
scanf("%d", &m);
while (m--){
scanf("%d %d", &a, &b);
lft.gogo(a, b);
}
}
return ;
}
zoj 2334 Monkey King/左偏树+并查集的更多相关文章
- HDU 1512 Monkey King (左偏树+并查集)
题意:在一个森林里住着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 ...
- 洛谷 - P1552 - 派遣 - 左偏树 - 并查集
首先把这个树建出来,然后每一次操作,只能选中一棵子树.对于树根,他的领导力水平是确定的,然后他更新答案的情况就是把他子树内薪水最少的若干个弄出来. 问题在于怎么知道一棵子树内薪水最少的若干个分别是谁. ...
- 洛谷 - P3377 - 【模板】左偏树(可并堆) - 左偏树 - 并查集
https://www.luogu.org/problemnew/show/P3377 左偏树+并查集 左偏树维护两个可合并的堆,并查集维护两个堆元素合并后可以找到正确的树根. 关键点在于删除一个堆的 ...
- hdu 1512 Monkey King 左偏树
题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...
- ZOJ2334 Monkey King 左偏树
ZOJ2334 用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用. 左偏树代码的核心也是两棵树的合并! 代码有些细节需要注意. #include&l ...
- HDU 1512 Monkey King(左偏树+并查集)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1512 [题目大意] 现在有 一群互不认识的猴子,每个猴子有一个能力值,每次选择两个猴子,挑出他们所 ...
- HDU1512 ZOJ2334 Monkey King 左偏树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...
- hdu 1512 Monkey King —— 左偏树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...
随机推荐
- 慕课网-安卓工程师初养成-3-4 Java中的比较运算符
来源:http://www.imooc.com/code/1299 比较运算符用于判断两个数据的大小,例如:大于.等于.不等于.比较的结果是一个布尔值( true 或 false ). Java 中常 ...
- C# 枚举显示中文
转自:http://www.cnblogs.com/yank/archive/2011/09/08/EnumDisplayInChinese.html using System; using Sy ...
- 【Spring 2】spring的属性注入形式
一.注入简介 spring是一个java bean的容器,它摒弃了过去通过new关键字调用类再调用类的实例的形式,通过依赖注入维护者一系列的java bean的示例.通过spring所提供的依赖注入 ...
- cordova local notification plugin
cordova plugin add org.apache.cordova.device cordova plugin add https://github.com/katzer/cordova-pl ...
- vba 快速定位当前EXCEL最后一栏
工作的需要,有时会对EXCEL数据进行处理,比如格式化,有数据的单元格画横线. 最初,傻傻的,直接用个循环从第1行,一直往后找,判断是否为空,真傻. Function FindLastCell() D ...
- 【C#基础】static 关键字用法小结
静态变量 当我们编写一个类时,其实就是在描述其对象的属性和行为,而并没有产生实质上的对象,只有通过new关键字才会产生出对象,这时系统才会分配内存空间给对象,其方法才可以供外部调用. 有时候,我们希望 ...
- 关于module_param()宏
在用户态下编程可以通过main()的来传递命令行参数,而编写一个内核模块则通过module_param () module_param宏是Linux 2.6内核中新增的,该宏被定义在include/l ...
- hbase blocksize设置,与hdfs关系
关于如何设定数据块的大小,我们应用一段HFile源码中的注释: 我们推荐将数据块的大小设置为8KB至1MB.大的数据块比较适合顺序的查询(比如Scan),但不适合随机查询,想想看,每一次随机查询可能都 ...
- Excel 统计IP
参考资料: 1:http://zhidao.baidu.com/question/127624244.html 其中的公式改成<1就可以了. 2:http://support.office.mi ...
- OpenJudge 2809 计算2的N次方
1.链接地址: http://bailian.openjudge.cn/practice/2809/ 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 任意给定一个正整数N(N ...