zoukankan      html  css  js  c++  java
  • 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
    ",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;
    }
  • 相关阅读:
    background-position 使用方法具体介绍
    Android平台上直接物理内存读写漏洞的那些事
    自己编写高负荷測试的工具
    String,StringBuffer与StringBuilder的差别??
    shell之here文档
    心跳检测的思路及代码
    高可用架构篇--MyCat在MySQL主从复制基础上实现读写分离
    MySQL主从复制之Mycat简单配置和高可用
    Mycat 读写分离+分库分表
    MyCat:对MySQL数据库进行分库分表
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8228470.html
Copyright © 2011-2022 走看看