HDU1512 Monkey King 左偏樹

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

合併兩個節點所在的集合,集合中最大值值減半,合併後輸出新的最大值。
左偏樹模板題,每次合併前彈出大根堆頂點,減半後再合並進去

    #include <iostream>
    #include <stdio.h>
    #include <map>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <math.h>
    #include <string.h>
    using namespace std;
    typedef long long ll;
    int mo[4][2]={0,1,1,0,0,-1,-1,0};
    const int MAXN=0x3f3f3f3f;
    const int sz=100005;
    int n,m;
    struct node{
        ll val,lc,rc,dis,par;
    }t[sz];

    void init(){
        for(int i=1;i<=n;i++){
            t[i].lc=t[i].rc=t[i].dis=0;
            t[i].par=i;
        }
    }

    int fid(int x){
        if(t[x].par==x) return x;
        else return t[x].par=fid(t[x].par);
    }

    int same(int x,int y){
        return fid(x)==fid(y);
    }

    int mer(int x,int y){
        if(x==0) return y;
        if(y==0) return x;
        if(t[x].val<t[y].val) swap(x,y);
        t[x].rc=mer(t[x].rc,y);
        t[t[x].rc].par=x;
        if(t[t[x].lc].dis<t[t[x].rc].dis)
            swap(t[x].lc,t[x].rc);
        if(t[x].rc==0)
            t[x].dis=0;
        else
            t[x].dis=t[t[x].rc].dis+1;
        return x;
    }

    int pop(int x){
        int l=t[x].lc,r=t[x].rc;
        t[x].lc=t[x].rc=t[x].dis=0;
        t[l].par=l;t[r].par=r;
        return mer(l,r);
    }

    int main()
    {
        int a,b,c,d,tmp;
        //freopen("r.txt","r",stdin);
        while(scanf("%d",&n)!=EOF){
            for(int i=1;i<=n;i++){
                scanf("%d",&t[i].val);
            }
            init();
            scanf("%d",&m);
            for(int i=1;i<=m;i++){
                scanf("%d%d",&a,&b);
                if(same(a,b)){
                    printf("-1\n");
                }else{
                    c=t[a].par,d=t[b].par;
                    a=pop(c);b=pop(d);
                    t[c].val/=2;
                    t[d].val/=2;
                    tmp=mer(a,b);
                    tmp=mer(tmp,c);
                    tmp=mer(tmp,d);
                    printf("%d\n",t[tmp].val);
                }
            }
        }
        return 0;
    }
發佈了53 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章