原题链接: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. Quick Sort

    class Program { static int[] a = new int[] { 6, 1, 2, 7, 9, 3, 4, 5, 10, 8 }; int n; static void Mai ...

  2. iOS调试 LLDB

      LLDB是个开源的内置于XCode的具有REPL(read-eval-print-loop)特征的Debugger,其可以安装C++或者Python插件.   常用调试命令:   1.print命 ...

  3. Android基础总结(3)——UI界面布局

    Android的UI设计有好几种界面程序编写方式.大体上可分为两大类:一类是利用可视化工具来进行,允许你进行拖拽控件来进行布局:还有一类是编写xml文档来进行布局.这两种方法可以相互转换. 1.常见的 ...

  4. 使用Apache CXF开发WebServices服务端、客户端

    在前一篇的博客中,我使用Xfire1.x来开发了WebServies的服务端. 但是如果你访问Apache的官网,可以看到xfire已经被合并了. 最新的框架叫做CXF. Apache CXF = C ...

  5. Python 编程规范-----转载

    Python编程规范及性能优化 Ptyhon编程规范 编码 所有的 Python 脚本文件都应在文件头标上 # -*- coding:utf-8 -*- .设置编辑器,默认保存为 utf-8 格式. ...

  6. AX 条码打印

    AX 条码打印集成在BarCode类及其之类barcode*. 由子类的defaultFont方法指定字体属性. eg, BarcodeCode39 指定条码字体"BC C39 3 to 1 ...

  7. Windows phone 8 学习笔记(9) 集成(转)

    本节整理了之前并没有提到的Windows phone 8 系统相关集成支持,包括选择器.锁定屏幕的.联系人的访问等.选择器列举了若干内置应用提供的相关支持:锁定屏幕展示了我们可以对锁定屏幕提供背景图像 ...

  8. nginx之keepalive

    一:设置 keepalive_timeout  0; 发curl: [xxx ~]$ curl -H "Keep-Alive: 60" -H "Connection: k ...

  9. UTF-8 GBK GB2312 之间的区别和关系

    UTF-8:Unicode TransformationFormat-8bit,允许含BOM,但通常不含BOM.是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三 ...

  10. GUI_DOWNLOAD参数说明

    对FUNCTION: GUI_DOWNLOAD中某些参数的用法.       call function 'GUI_DOWNLOAD'     exporting *     BIN_FILESIZE ...