Hdu 1512 Monkey King 左偏树 解题报告

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(N<=10000)只猴子。在一开始,他们是互不认识的。但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识(认识具有传递性)的两只猴子之间。争斗时,两只猴子都会请出他认识的猴子里最强壮的一只(有可能是他自己)进行争斗。争斗后,这两只猴子就互相认识。每个猴子有一个强壮值,但是被请出来的那两只猴子进行争斗后,他们的强壮值都会减半(例如10会减为5,5会减为2)。现给出每个猴子的初始强壮值,给出M次争斗,如果争斗的两只猴子不认识,那么输出争斗后两只猴子的认识的猴子里最强壮的猴子的强壮值,否则输出 -1。

思路

左偏树裸题,一开始一只猴子就是一棵左偏树,然后每次打架先将堆顶删除,力量值减半插入回原来的堆,再合并两个堆,最后输出堆顶即可。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> 
using namespace std;
const int INF=0x7fffffff;
const int N=100000+50;
struct node
{
    int key,dis;
    int lson,rson;
    int father; 
};
node ed[N];
int n;
void build(int x,int k)
{
    ed[x].key=k;
    ed[x].father=x;
    ed[x].lson=ed[x].rson=0;
    ed[x].dis=(x==0)?-1:0;
}
int find(int x)
{
    if (ed[x].father==x) return x;
    else return(find(ed[x].father));
}
int merge(int x,int y)
{
    if (x==0) return y;
    if (y==0) return x;
    if (ed[x].key<ed[y].key) swap(x,y);
    ed[x].rson=merge(ed[x].rson,y);
    int &l=ed[x].lson,&r=ed[x].rson;
    ed[l].father=ed[r].father=x;
    if (ed[l].dis<ed[r].dis) swap(l,r);
    if (r==0) ed[x].dis=0;
    else ed[x].dis=ed[r].dis+1;
    return x;
}
int del(int rt)
{
    int l=ed[rt].lson;
    int r=ed[rt].rson;
    ed[l].father=l;
    ed[r].father=r;
    ed[rt].dis=ed[rt].lson=ed[rt].rson=0;
    return merge(l,r);
}
int solve(int x,int y)
{
    ed[x].key>>=1;
    ed[y].key>>=1;
    int left,right;
    left=del(x);
    right=del(y);
    left=merge(left,x);
    right=merge(right,y);
    left=merge(left,right);
    return ed[left].key;
}
void init()
{
    for (int i=1;i<=n;i++)
    {
        int strong;
        scanf("%d",&strong);
        build(i,strong);
    }
    build(0,0);
}
void get_ans()
{
    int q;
    scanf("%d",&q);
    for (int i=0;i<q;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        int fx=find(x),fy=find(y);
        if (fx==fy) printf("-1\n"); 
        else printf("%d\n",solve(fx,fy));
    }
}
int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        init();
        get_ans();
    } 
    return 0;    
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值