原题链接: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/左偏树+并查集的更多相关文章

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

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

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

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

  3. 洛谷 - P1552 - 派遣 - 左偏树 - 并查集

    首先把这个树建出来,然后每一次操作,只能选中一棵子树.对于树根,他的领导力水平是确定的,然后他更新答案的情况就是把他子树内薪水最少的若干个弄出来. 问题在于怎么知道一棵子树内薪水最少的若干个分别是谁. ...

  4. 洛谷 - P3377 - 【模板】左偏树(可并堆) - 左偏树 - 并查集

    https://www.luogu.org/problemnew/show/P3377 左偏树+并查集 左偏树维护两个可合并的堆,并查集维护两个堆元素合并后可以找到正确的树根. 关键点在于删除一个堆的 ...

  5. hdu 1512 Monkey King 左偏树

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

  6. ZOJ2334 Monkey King 左偏树

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

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

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1512 [题目大意] 现在有 一群互不认识的猴子,每个猴子有一个能力值,每次选择两个猴子,挑出他们所 ...

  8. HDU1512 ZOJ2334 Monkey King 左偏树

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

  9. hdu 1512 Monkey King —— 左偏树

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

随机推荐

  1. 【LeetCode】17. Letter Combinations of a Phone Number

    题目: 思路:设置两个List,一个存储当前层,一个存储最终层 public class Solution { public List<String> letterCombinations ...

  2. 【测试】RAC搭建(裸设备)

    环境描述:   节点一 节点二 主机名 rac1 rac2 IP 192.168.10.11 192.168.10.12 IP-VIP 192.168.10.111 192.168.10.112 IP ...

  3. 无法定位程序输入点ucrtbase.

    转:http://tieba.baidu.com/p/3848709732 后的解决方法是需要安装VC 2015 Redistributable,请自己选择相应的版本.百度云盘共享地址:http:// ...

  4. Bootstrap 3支持IE 8遇到的一个小问题

    使用Bootstrap完成web的UI后,在IE 8运行时,发现.container等class的标签的的宽度并没按预期的宽度显示,本人已经根据bootstrap官方说明 http://getboot ...

  5. JS判断对象是否存在的方法

    Javascript语言的设计不够严谨,很多地方一不小心就会出错. 举例来说,请考虑以下情况. 现在,我们要判断一个全局对象myObj是否存在,如果不存在,就对它进行声明.用自然语言描述的算法如下: ...

  6. Vmware ESX5i 环境下部署Windows Storage Server 2008 R2

    ESX5i 环境下部署Windows Storage Server 2008 R2       Windows Storage Server 2008 这款产品微软早已发布,WSS2008是基于Win ...

  7. UNICODE字符串与多字节字符串的转换

    相互转换的两个函数的声明: 1. 多字节字符串与宽字符串的转换 int MultiByteToWideChar( UINT CodePage, // code page,一般设为 CP_ACP DWO ...

  8. openstack4j接口调试

    //import java.util.List;////import org.openstack4j.api.OSClient.OSClientV3;//import org.openstack4j. ...

  9. CentOS6.5 ssh远程连接缓慢解决方法

    UseDNS no GSSAPIAuthentication no 1.适用命令及方案如下:[远程连接及执行命令]ssh -p22 root@10.0.0.19ssh -p22 root@10.0.0 ...

  10. ostack