码迷,mamicode.com
首页 > 其他好文 > 详细

POJ ???? Monkey King

时间:2018-01-07 20:12:10      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:turn   include   ted   lines   case   scan   course   ++   get   

 

题目描述

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;
}

 

POJ ???? Monkey King

标签:turn   include   ted   lines   case   scan   course   ++   get   

原文地址:https://www.cnblogs.com/JYYHH/p/8228470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!