hdu 1512 Monkey King (左偏树可并堆 并查集)

来源:互联网 发布:工作流数据库设计思路 编辑:程序博客网 时间:2024/06/07 06:00

Monkey King

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6170 Accepted Submission(s): 2628

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个猴子,一开始每个猴子只认识自己。每个猴子有一个力量值,力量值越大表示这个猴子打架越厉害。如果2个猴子不认识,他们就会找他们认识的猴子中力量最大的出来单挑,单挑不论输赢,单挑的2个猴子力量值减半,这2拨猴子就都认识了,不打不相识嘛。现在给m组询问,如果2只猴子相互认识,输出-1,否则他们各自找自己认识的最厉害的猴子单挑,求挑完后这拨猴子力量最大值。

思路:
这题涉及到集合合并的操作,用并查集维护判断-1。其次要找到某一拨猴子中力量最大值,可以用堆维护。2拨猴子要合并而又不失堆的特性,最简单的可并堆就写左偏树吧。

这题就要求维护最大值,所以维护一个大根堆。

简单说说吧(copy)
其次左偏树顾名思义还有左偏的性质。这里的左偏不是说树的左子树就一定比右子树大。左偏树每个节点除了堆的关键字key外,还有一个关键字dis,距离,这是左偏树的特色。左偏树定义这样一类节点:如果一个节点的左右子树有空树,称之为外点,树根到一个外点的最近距离为dis。比如一颗叶子节点的dis为0。左偏树就是根据dis值来调整的,添加的时候在右子树,这样就可以让堆不会退化得太厉害。左偏树保证左偏的性质:节点的左子树的dis值要不小于右子树的dis值。也就是说左偏树从根节点一路向右能最快访问到外点。

所以左偏树其实就是一颗有左偏性质的堆有序二叉树。

左偏树并不是一颗平衡树。因为左偏树的主要功能是快速访问最值节点以及修改后的快速恢复。所以左偏树放弃了平衡 的性质。相反,一颗平衡的左偏树反而会影响左偏树的性能。

关于这题,用一个并查集维护猴子的集合,左偏树用来快速合并,并且快速保持大堆性质。并查集的并操作其实可以在左偏树合并的时候进行。

#include <iostream>  #include <cstdio>  #include <cstring>#include <algorithm>  using namespace std;const int N = 100005;  int m, n;  int set[N];struct node{    int l, r, dis, key;  }tree[N];  int getfather(int x){//    if(x == set[x]) return x;    return set[x] = getfather(set[x]);}int merge(int a, int b){    if( !a ) return b;//如果一边为空,直接返回就行    if( !b ) return a;    if(tree[a].key < tree[b].key) swap(a, b);//维护大根堆    tree[a].r = merge(tree[a].r, b);//递归合并堆    set[tree[a].r] = a;    if(tree[tree[a].l].dis < tree[tree[a].r].dis)        swap(tree[a].l, tree[a].r);//保证左偏    if(tree[a].r) tree[a].dis = tree[tree[a].r].dis + 1;//更新dis     else tree[a].dis = 0;    return a;}int pop(int a){//取最大的出来     int l = tree[a].l;    int r = tree[a].r;    set[l] = l; set[r] = r;    //因为要暂时删掉根,所以左右子树先作为根    tree[a].l = tree[a].r = tree[a].dis = 0;    return merge(l, r);}int main(){    int a, b;    while( ~scanf("%d", &n) ){        for(int i=1; i<=n; i++){            scanf("%d", &tree[i].key);            set[i] = i;            tree[i].l = tree[i].r = tree[i].dis = 0;          }        scanf("%d", &m);        while( m-- ){            scanf("%d%d", &a, &b);            int ra = getfather(a), rb = getfather(b);            if(ra == rb) printf("-1\n");            else{                int aa = pop(ra);//删除最大,左右子树合并                tree[ra].key /= 2;                ra = merge(aa, ra);//重新插入                int bb = pop(rb);                tree[rb].key /= 2;                rb = merge(bb, rb);                printf("%d", tree[merge(ra, rb)].key);                putchar(10);//换行            }        }    }    return 0;  }
原创粉丝点击