左偏树自己的一点理解【hdu1512】【Monkey King】

这里写图片描述
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34693563
向大(hei)佬(e)势力学(di)习(tou)

前段时间学的左偏树,今天复习了一遍,写篇博客加深印象。

左偏树是可合并堆中好写好理解的数据结构。其定义为:
1、满足堆的性质
2、节点的左子节点的距离不小于右子节点的距离
3、每一个子树也满足左偏树的性质
如图
这里写图片描述

然后这个“距离”懵逼了我好久。标准的定义是:
到最近的右儿子为空的节点的距离

可是单是知道定义,到底该怎么处理呢?直观感觉难查询难计算。那么,我们将其维护成某些特殊的状态:一直向右就是右子节点,就方便计算和维护了,于是还多了一条性质:”节点的距离等于它的右子节点的距离加1“,这个性质对于维护来说即为重要。
这里有一个小技巧:把空节点的dis设为-1,这样在维护的时候就不用分情况讨论了。

a[x].dis=a[a[x].rs].dis+1;

当在合并时,可能会出现左子节点距离小于右子节点距离的情况,这时只需要将左右儿子交换即可

if(a[a[x].ls].dis<a[a[x].rs].dis) swap(a[x].ls,a[x].rs);

现在重头戏来了,如何合并?
借用某大大的图解,我也是看了之后才理解到的
http://www.cnblogs.com/yc_sunniwell/archive/2010/06/28/1766756.html
这里写图片描述
这里写图片描述
这里写图片描述
由图可以看出,合并过程其实更像是“插入”过程,是将一棵树插入另一棵树。一直向右边走,以堆的性质为条件判断是谁插入谁。
由于维护了左偏的性质,dis最多就log层,所以时间上是不会爆的。

下面是最好模板化的代码

int merge(int x,int y){
    if(x==0) return y;
    if(y==0) return x;
    if(a[x].key<a[y].key){
        swap(x,y);
    } 
    a[x].rs=merge(a[x].rs,y);
    if(a[a[x].ls].dis<a[a[x].rs].dis) swap(a[x].ls,a[x].rs);
    a[x].dis=a[a[x].rs].dis+1;
    return x;
}

插入和删除就很好写的,只不过是对merge进行一些花样操作
插入一个点,可以把这个点看做一棵树,merge就好了
删除其实就是把左右子树合并起来

好了,基础操作总结完了
然而左偏树常常与并查集连用,我认为把祖先指向堆顶元素会方便一些,但在处理时还是有点绕

下面由一道入门题结束吧

Problem Description
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.
Sample Input
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
Sample Output
8
5
5
-1
10
题目大意
有n只猴子,每只猴子有厉害值,一开始素不相识。
两只不熟的猴子相遇,它们会发生争执。然后,它们会邀请它们认识的最厉害的猴子决斗。决斗完这两只决斗的猴子的厉害值都会减半。决斗能促进友谊,这样这两拨素不相识的猴子就都认识了对方。
如果一只猴子不认识任何其他猴子,那么它就会亲自上阵。
每次给出两只猴子x,y,判断它们是否认识对方。若不认识,输出决斗后它们共同所在猴子群体中最厉害猴子的厉害值。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=100000+5;

struct Node {
    int ls,rs;
    int dis,key;
}a[N];
int fa[N],str[N],n,m;

int getfa(int x){
    if(fa[x]==x) return x;
    return fa[x]=getfa(fa[x]);
}
int merge(int x,int y){
    if(x==0) return y;
    if(y==0) return x;
    if(a[x].key<a[y].key){
        swap(x,y);
    } 
    a[x].rs=merge(a[x].rs,y);
    if(a[a[x].ls].dis<a[a[x].rs].dis) swap(a[x].ls,a[x].rs);
    a[x].dis=a[a[x].rs].dis+1;
    return x;
}
int main(){
    a[0].ls=a[0].rs=0;
    a[0].dis=a[0].key=-1;

    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++){
            scanf("%d",&str[i]);
            fa[i]=i;
            a[i].dis=0,a[i].key=str[i];
            a[i].ls=a[i].rs=0;
        } 
        scanf("%d",&m);
        while(m--){
            int aa,bb;
            scanf("%d%d",&aa,&bb);
            int faa=getfa(aa),fab=getfa(bb);
            if(faa==fab){
                printf("-1\n");continue;
            } 
            a[faa].key/=2,a[fab].key/=2;
            int tmpa=merge(a[faa].ls,a[faa].rs),tmpb=merge(a[fab].ls,a[fab].rs);
            a[faa].ls=a[faa].rs=a[fab].ls=a[fab].rs=0;
            fa[faa]=merge(faa,tmpa),fa[fab]=merge(fab,tmpb);
            fa[fa[faa]]=fa[faa],fa[fa[fab]]=fa[fab];
            faa=getfa(faa),fab=getfa(fab);
            int tmp=merge(faa,fab);
            fa[faa]=fa[fab]=tmp;
            printf("%d\n",a[tmp].key);
        }
    }
    return 0;
}
posted @ 2017-10-31 19:09  LinnBlanc  阅读(93)  评论(0编辑  收藏