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 ...
随机推荐
- oracle的sqlldr并行导入表不要加索引
ORA-26002: Table string has index defined upon it. Cause: Parallel load was specified into a table w ...
- dwr与ssh框架整合教程
(1)dwr与ssh框架整合教程dwr框架介绍. DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开 发人员开发包含AJ ...
- vimdiff vimmerge 配置及使用
1 Set up vimdiff The vimdiff as a merge tool will display several buffers to show the yours/theirs/o ...
- 华为OJ平台——统计字符串中的大写字母
题目描述: 统计字符串中的大写字母的个数 输入: 一行字符串 输出: 字符串中大写字母的个数(当空串时输出0) 思路: 这一题很简单,直接判断字符串中的每一个字符即可,唯一要注意的一点是输入的字符串可 ...
- java基础回顾(六)——WeakReference、SoftReference
在Java里, 当一个对象o被创建时, 它被放在Heap里. 当GC运行的时候, 如果发现没有任何引用指向o, o就会被回收以腾出内存空间. 或者换句话说, 一个对象被回收, 必须满足两个条件: 1) ...
- HTML你应该知道的三大基本元素
顶级.块级.内联,html元素的三大分类 如果将这些元素细分, 又可以分别归为顶级(top-level)元素,块级(block-level)元素和内联(inline)元素. 1. Top-level ...
- 【IHttpHandler】在ASP.Net2.0中使用UrlRewritingNet实现链接重写
很多时候我们需要链接转向(Url Rewriting),例如二级域名转向.文章访问链接等场合. 让我们看两个例子: 1 你现在看到的当前作者的博客园的域名: http://jx270.cnblogs. ...
- servlet 启动加载配置文件及初始化
在servlet开发中,会涉及到一些xml数据的读取和一些初始化方法的调用.可以在tomcat启动的时候,加载一个servlet去初始化一些数据. 摘自 http://stone02111.iteye ...
- Known plaintext attack
When you find a ZIP/RAR file with password protected in the evidence, you may try dictionary attack ...
- 如何在某个按钮上触发 bootstarp Modal 组件
<?php Modal::begin([ 'id'=>'myModal', 'header' => '<h2>标题</h2>']); echo '内容'; M ...