HDU1512 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只猴子,每只猴子有厉害值,一开始素不相识。
两只不熟的猴子相遇,它们会发生争执。然后,它们会邀请它们认识的最厉害的猴子决斗。决斗完这两只决斗的猴子的厉害值都会减半。决斗能促进友谊,这样这两拨素不相识的猴子就都认识了对方。
如果一只猴子不认识任何其他猴子,那么它就会亲自上阵。
每次给出两只猴子x,y,判断它们是否认识对方。若不认识,输出决斗后它们共同所在猴子群体中最厉害猴子的厉害值。
我们要把几个猴子串联起来,这只能靠并查集。如果我们要找出两群猴子中战斗值最大的那个,显然我们用堆是最划算的。此外,我们也应该看出,打完了要合并,如果要用堆的话,这只能是可并堆。
藉此我们不难套模板写出代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100000;
struct node
{
    int dis,lson,rson,key;
}tree[N+5];
int father[N+5];
int n,m,x,y;
int find(int x)
{
    int root=x;
    while(root!=father[root]) root=father[root];
    int fa=father[x];
    while(fa!=root)
    {
        father[x]=root;  
        x=fa;  
        fa=father[x];  
    }
    return root;
}
int merge(int x,int y)
{
    if(!x)return y;
    if(!y)return x;
    if(tree[x].key<tree[y].key)swap(x,y);
    tree[x].rson=merge(tree[x].rson,y);
    father[tree[x].rson]=x;
    if(tree[tree[x].lson].dis<tree[tree[x].rson].dis)
    swap(tree[x].lson,tree[x].rson);
    if(tree[x].rson)tree[x].dis=tree[tree[x].rson].dis+1;
    else tree[x].dis=0;
    return x;
}
int pop(int x)
{
    int lf=tree[x].lson;
    int rg=tree[x].rson;
    father[lf]=lf;
    father[rg]=rg;
    tree[x].lson=tree[x].rson=tree[x].dis=0;
    return merge(lf,rg);
}
int main()
{
    while(~scanf("%d",&n))
    {
        memset(tree,0,sizeof(tree));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&tree[i].key);
            father[i]=i;
        }
        for(scanf("%d",&m);m;m--)
        {
            scanf("%d%d",&x,&y);
            int u=find(x),v=find(y);
            if(u==v){printf("-1\n");continue;}
            int X=pop(u);
            tree[u].key/=2;
            u=merge(X,u);
            int Y=pop(v);
            tree[v].key/=2;
            v=merge(Y,v);
            printf("%d\n",tree[merge(u,v)].key);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值