hdu1512:Monkey King(左偏树)

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,否则他们各自找自己认识的最牛叉的猴子单挑,求挑完后这拨猴子力量最大值。

题解:左偏树
每次合并两个大根堆:将堆顶子树合并,并对堆顶元素操作,之后合并。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
const int Maxn=1e5+50;
inline int read()
{
    char ch=getchar();int i=0,f=1;
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){i=(i<<3)+(i<<1)+ch-'0';ch=getchar();}
    return i*f;
}
struct node
{
    node *lc,*rc;
    int val,dist;
    node(){}
    node(const int &v):lc(NULL),rc(NULL),val(v),dist(0){}
}*tr[Maxn],POOL[Maxn],*pool=POOL;
int n,Q,w[Maxn],anc[Maxn];
inline node* newnode(node *&x,const int &v)
{
    x=++pool;
    x->lc=x->rc=NULL;
    x->val=v;
    x->dist=0;
}
inline node* merge(node *x,node *y)
{
    if(!x)return y;
    if(!y)return x;
    if(x->val<y->val)swap(x,y);
    x->rc=merge(x->rc,y);
    if(!x->lc||x->lc->dist<x->rc->dist)swap(x->lc,x->rc);
    x->dist=x->rc?x->rc->dist+1:0;
    return x;
}
inline int getf(int x)
{
    if(anc[x]==x)return x;
    return anc[x]=getf(anc[x]);
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
    pool=POOL;
    for(int i=1;i<=n;i++)w[i]=read(),newnode(tr[i],w[i]),anc[i]=i;
    Q=read();
    while(Q--)
    {
        int x=read(),y=read();
        x=getf(x),y=getf(y);
        if(x==y)printf("-1\n");
        else
        {
            anc[x]=y;
            node *tmp=merge(tr[x]->lc,tr[x]->rc);
            node *tmp2=merge(tr[y]->lc,tr[y]->rc);
            tmp=merge(tmp,tmp2);
            tr[x]->lc=tr[x]->rc=tr[y]->lc=tr[y]->rc=NULL;
            tr[x]->val/=2,tr[y]->val/=2;
            tr[x]->dist=tr[y]->dist=0;
            tr[y]=merge(tr[y],merge(tr[x],tmp));
            printf("%d\n",tr[y]->val);
        }
    }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值