POJ ???? 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.

输入输出格式

输入格式:

 

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.

有多组数据

 

输出格式:

 

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strength value of the strongest monkey among all of its friends after the duel.

 

输入输出样例

输入样例#1: 
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
输出样例#1: 
8
5
5
-1
10



说明

题目可能有多组数据

 

 

懒得去POJ找原题啦,随便粘了一下洛谷的。


顺手再练一下可并堆。。。。
洛谷上的翻译有毒,应该是每次打架只有厉害的那个猴子战斗力减半。

 

#include<bits/stdc++.h>
#define ll long long
#define maxn 100005
using namespace std;
int f[maxn],ch[maxn][2];
int val[maxn],dis[maxn];
int n,m;

inline int get_fa(int x){
    while(f[x]) x=f[x];
    return x;
}

int merge(int x,int y){
    if(!x||!y) return x+y;
    if(val[x]<val[y]) swap(x,y);
    
    ch[x][1]=merge(ch[x][1],y);
    f[ch[x][1]]=x;
    if(dis[ch[x][1]]>dis[ch[x][0]]) swap(ch[x][1],ch[x][0]);
    dis[x]=dis[ch[x][1]]+1;
    
    return x;
}

inline void work(int x,int y){
    int fa=get_fa(x),fb=get_fa(y);
    int nowx,nowy;
    
    if(fa==fb){
        puts("-1");
        return;
    }
    
    printf("%d\n",max(val[fa],val[fb])>>1);
    if(val[fa]>val[fb]) val[fa]>>=1;
    else val[fb]>>=1;
    
    f[ch[fa][0]]=f[ch[fa][1]]=0;
    nowx=merge(ch[fa][0],ch[fa][1]);
    ch[fa][0]=ch[fa][1]=0;
    nowx=merge(nowx,fa);

    f[ch[fb][0]]=f[ch[fb][1]]=0;
    nowy=merge(ch[fb][0],ch[fb][1]);
    ch[fb][0]=ch[fb][1]=0;
    nowy=merge(nowy,fb);    
    
    merge(nowx,nowy);
}

int main(){
    while(scanf("%d",&n)==1&&n){
        memset(f,0,sizeof(f));
        memset(dis,0,sizeof(dis));
        memset(ch,0,sizeof(ch));
        for(int i=1;i<=n;i++) scanf("%d",val+i);
        scanf("%d",&m);
        int uu,vv;
        while(m--){
            scanf("%d%d",&uu,&vv);
            work(uu,vv);
        }
    }
    
    return 0;
}

 

posted @ 2018-01-07 17:09  蒟蒻JHY  阅读(223)  评论(0编辑  收藏  举报