题目链接:HDU - 1512

Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can't avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.
Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).
And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.
Input
There are several test cases, and each case consists of two parts.
First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).
Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.
Output
For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.
 
题意描述:有n只猴子,每只猴子有一个值,两只猴子如果打架的话,他们的值各自掉一半。给出m个事件,每个事件给出两只猴子,如果两只猴子不认识的话,打完架就成为了朋友(两只猴子的各自朋友也都互相成为了朋友),求出打完架后两只猴子的所有朋友中值最大的。
算法分析:这道题我刚开始做的时候,想到了并查集,以为这就够了(其实时间复杂度我也不敢直视),交上去果断TLE了,后来看了讨论里有说,尼玛,这就是传说中的左偏树的题目啊,果断得好好学习一下,弥补一下自己的数据结构的知识。
说明:头一次搞左偏树,代码是借鉴别人的,不过真心写的比较好就拿来了。同时,推荐一下集训队的论文,基本上看了论文后就对左偏树有了一定的了解了。

左偏树的特点及其应用

 /*左偏树*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn = +; int father[maxn];
struct node
{
int l,r;
int dis;
int strong;
}LTree[maxn];
int Find(int x)
{
if (father[x]==x) return x;
return father[x]=Find(father[x]);
}
int Merge(int x,int y)
{ //返回合并后的根
if (x==) return y;
if (y==) return x;
if (LTree[x].strong < LTree[y].strong) //大顶堆
swap(x,y);
LTree[x].r = Merge(LTree[x].r,y); //递归合并右子树和Y
int l = LTree[x].l , r = LTree[x].r;
father[r] = x; //更新T右子树的根
if (LTree[l].dis < LTree[r].dis) //维护堆性质
swap(LTree[x].l,LTree[x].r);
if (LTree[x].r == ) //如果没有右子树 则距离为0
LTree[x].dis = ;
else
LTree[x].dis = LTree[LTree[x].r].dis + ;
return x;
}
int del(int x)
{ //返回删除根以后左右子树的合并的根
int l,r;
l=LTree[x].l;
r=LTree[x].r;
father[l]=l;
father[r]=r;
LTree[x].l=LTree[x].r=LTree[x].dis=;
return Merge(l,r);
}
void solve(int x,int y)
{
LTree[x].strong /= ;
LTree[y].strong /= ;
//问每次PK以后,当前这个群体里力量最大的猴子的力量是多少。
int left,right;
left = del(x);
right = del(y);
left = Merge(left,x);
right = Merge(right,y);
left = Merge(left,right);
printf("%d\n",LTree[left].strong);
}
int main()
{
int n,m,x,y;
while (scanf("%d",&n)!=EOF)
{
for (int i= ;i<=n ;i++)
{
scanf("%d",&LTree[i].strong);
LTree[i].l=;
LTree[i].r=;
LTree[i].dis=;
father[i]=i; //起始已自己为父亲
}
scanf("%d",&m);
for (int i= ;i<=m ;i++)
{
scanf("%d%d",&x,&y);
int fx=Find(x),fy=Find(y);
if (fx == fy) printf("-1\n");
else solve(fx,fy);
}
}
return ;
}

hdu 1512 Monkey King 左偏树的更多相关文章

  1. hdu 1512 Monkey King —— 左偏树

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

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

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

  3. HDU 1512 Monkey King ——左偏树

    [题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...

  4. HDU 1512 Monkey King(左偏堆)

    爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...

  5. ZOJ2334 Monkey King 左偏树

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

  6. zoj 2334 Monkey King/左偏树+并查集

    原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...

  7. HDU1512 ZOJ2334 Monkey King 左偏树

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

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

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

  9. LuoguP1456 Monkey King (左偏树)

    struct LeftTree{ int l,r,val,dis; }t[N]; int fa[N]; inline int Find(int x){ return x == fa[x] ? x : ...

随机推荐

  1. python-configparser模块,xml.etree模块

    操作键值对文件 #文件db格式为 [section] a = 1 b = 2 [section1] d = 3 c = 4 import configparser #获取所有节点 config = c ...

  2. ES原理(转载)

    该博客属于转载,是很经典的一篇关于ES的介绍: Elasticsearch 是一个兼有搜索引擎和NoSQL数据库功能的开源系统,基于Java/Lucene构建,可以用于全文搜索,结构化搜索以及近实时分 ...

  3. 爬虫:Scrapy13 - 发送 email

    虽然 Python 通过 smtplib 库使得发送 email 变得非常简单,Scrapy 仍然提供了自己的实现.该功能十分易用,同时由于采用了 Twisted 非阻塞式(non-blocking) ...

  4. WeUI 在小程序中使用

    才接触小程序.想找个ui框架..也不知道咋弄: 下载地址:点击打开链接 将weui-wxss-master\dist\style\weui.wxss文件导入到小程序项目的根目录下 引入weui.wxs ...

  5. MyBatis:SQL语句中的foreach标签的详细介绍

    foreach 也就是遍历迭代,在SQL中通常用在 in 这个关键词的后面 foreach元素的属性主要有 item,index,collection,open,separator,close. 分别 ...

  6. java中unmodifiableList方法的应用场景

    java对象中primitive类型变量可以通过不提供set方法保证不被修改,但对象的List成员在提供get方法后,就可以随意add.remove改变其结构,这不是希望的结果.网上看了下,发现Col ...

  7. [NOI2015][bzoj4197] 寿司晚宴 [状压dp+质因数]

    题面 传送门 思路 首先,要让两个人选的数字全部互质,那么有一个显然的充要条件:甲选的数字的质因数集合和乙选的数字的质因数集合没有交集 30pt 这种情况下n<=30,也就是说可用的质数只有10 ...

  8. session-cookie 和token登录验证

    最近研究了下基于token的身份验证,并将这种机制整合在个人项目中.现在很多网站的认证方式都从传统的seesion+cookie转向token校验.对比传统的校验方式,token确实有更好的扩展性与安 ...

  9. c#网络编程-第一章

    1.需求 获得网页数据,并填充到webbrowser空间中 2.代码示例 private void button1_Click_1(object sender, EventArgs e) { //1. ...

  10. Mysql性能优化【转】

    mysql的性能优化无法一蹴而就,必须一步一步慢慢来,从各个方面进行优化,最终性能就会有大的提升. Mysql数据库的优化技术 对mysql优化是一个综合性的技术,主要包括 表的设计合理化(符合3NF ...