http://acm.hdu.edu.cn/showproblem.php?pid=1512

题意:

有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了,否则的话,这两只都会从它们所认识的猴子中派出一只力量值最大的猴子出来,并且这只猴子的力量值会减半,在打过之后,这两只猴子所在的集体就都认识了。

思路:
这是一道左偏树的模板题。

每个节点有5个值,l为左儿子节点,r为右儿子节点,fa为父亲节点(父亲节点的用法与并查集相似),val为优先级(这道题目就是大根堆),dis是距离值(在两棵树合并的时候使用,根据dis来决定左右子树是否需要对换)。

一开始每只猴子就是一棵树,每次决斗就是将两棵树进行合并。

 #include<iostream>
#include<cstdio>
using namespace std;
const int maxn = +; int n, m; struct Heap
{
int l,r,fa,val,dis;
}t[maxn]; int finds(int x)
{
return t[x].fa == -? x:t[x].fa = finds(t[x].fa);
} int merge(int x, int y)
{
if(x == ) return y; //如果为0的话,就说明是空子树,根节点当然就是另一节点了
if(y == ) return x;
if(t[y].val>t[x].val) swap(x,y); //始终往右子树进行插入
t[x].r = merge(t[x].r,y);
t[t[x].r].fa = x;
if(t[t[x].l].dis < t[t[x].r].dis) swap(t[x].l,t[x].r); //是否需要左右子树的对换,这样是为了右子树尽量短
if(t[x].r == ) t[x].dis = ; //距离的重新分配
else t[x].dis = t[t[x].r].dis + ;
return x;
} int pop(int &root)
{
int l = t[root].l;
int r = t[root].r;
t[root].l = t[root].r = t[root].dis = ;
t[root].fa = -;
t[l].fa = t[r].fa = -; //删除root根节点
return merge(l,r); //这样一来相当于分裂成了两棵子树,重新进行合并,最后返回值为合并后的根节点
} int push(int x, int y)
{
return merge(x,y);
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
{
t[i].l=t[i].r=t[i].dis=;
t[i].fa=-;
scanf("%d",&t[i].val);
}
scanf("%d",&m);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
int x=finds(a);
int y=finds(b);
if(x!=y)
{
t[x].val/=;
int xx = push( pop(x),x);
t[y].val/=;
int yy = push( pop(y),y);
printf("%d\n",t[merge(xx,yy)].val);
}
else puts("-1");
}
}
return ;
}

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 ...

随机推荐

  1. 数据集成工具Kettle、Sqoop、DataX的比较

    数据集成工具很多,下面是几个使用比较多的开源工具. 1.阿里开源软件:DataX         DataX 是一个异构数据源离线同步工具,致力于实现包括关系型数据库(MySQL.Oracle等).H ...

  2. [转载]WebService使用的一些总结

    什么是WebService: 这个不用我在这里废话,网上的资料一搜一大把,如果你没有接触过这方面的知识,你可以先去网上查一下.这里我只想说一下我印象比较深刻的几点: WebService是基于soap ...

  3. zabbix 服务端安装(server)

    zabbix版本:Zabbix 2.2 LTS 备注:Linux下安装zabbix需要有LAMP或者LNMP运行环境 准备篇: 一.Web环境:Nginx+MySQL+PHP CentOS 7.0编译 ...

  4. Talented Chef ZOJ - 3778

    As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same time. ...

  5. mysql复习之一

    DROP DATABASE mysql_shiyan;. cd /home/shiyanlou/Desktop git clone https://github.com/shiyanlou/SQL4 ...

  6. 框架frame

    使用框架切分网页 效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...

  7. c语言cgi笔记

    直接输出接收的数据 #include <stdio.h>#include <stdlib.h>main(){int i,n;printf ("Content-type ...

  8. 20145221高其_Web安全基础实践

    20145221高其_Web安全基础实践 目录 实践目标 WebGoat BurpSuite Injection Flaws Cross-Site Scripting (XSS) 总结 实践目标 (1 ...

  9. bzoj 1014 火星人prefix - 链表 - 分块

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  10. day 26 元类

    一.isinstance issubclass class Person: passclass Student(Person): passstu1=Student()#判断是不是实例print(isi ...