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

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

时间: 2024-11-04 03:08:25

POJ ???? Monkey King的相关文章

POJ 1364 King --差分约束第一题

题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析:典型差分约束题,变换,令Ti = SUM(Xj) (0<=j<=i).  则表达式(1)可以看做T(a+b)-T(a-1) > k,也就是T(a-1)-T(a+b) < -k,又因为全是整数,所以T(a-1)-T(a+b) <= -k-1.  同理,(2)看做T(a+b)-T(

poj 1904 King&#39;s Quest

King's Quest 题意:有N个王子和N个妹子;(1 <= N <= 2000)第i个王子喜欢Ki个妹子:(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每一个王子可以有几种选择(在自己喜欢的妹子里面选),并输出可选的妹子的标号(升序): Sample Input 4 (N) 2 1 2 (Ki) 2 1 2 2 2 3 2 3 4 1 2 3 4 (完美匹配) Sample Output 2 1 2 2 1 2 1 3 1 4 分析:图匹配问题,1~N为王子的

数据结构(左偏树):HDU 1512 Monkey King

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4714    Accepted Submission(s): 2032 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they e

Poj 1904 King&#39;s Quest 强连通分量

题目链接: http://poj.org/problem?id=1904 题意: 有n个王子和n个公主,王子只能娶自己心仪的公主(一个王子可能会有多个心仪的公主),现已给出一个完美匹配,问每个王子都可以取哪些公主,并且保证取了一个公主后,全局还是存在完美匹配. 题解: 1.建图: 如果王子u对公主v心仪,则连一条边u->v.在样例给出的那组完美匹配中,如果王子u娶了公主v,连一条边v->u. 2.求强连通分量: 如果王子和自己心仪的公主属于同一个强连通分量,那么王子就可以娶这个公主. 1 #i

POJ - 1904 King&#39;s Quest(强连通分量+二分图匹配)

题目大意:有N个帅哥和N个美女,现在给出每个帅哥所喜欢的美女的编号,和一个帅哥和美女的完美匹配 问每个帅哥可以娶多少个美女,且当他娶完这个美女后,剩下的人还可以完美匹配 解题思路:神题啊,给一个大神的详细解答 具体是这样的,首先先建边,把帅哥和能娶到的美女连边,再把完美匹配的美女和帅哥连边,这样就形成了一张有向图了 接着,找出这张有向图的所有强连通分量,在强连通分量里面的帅哥都可以娶到自己喜欢的美女,且娶完这个美女后,不会影响到其他人 为什么呢? 假设xi为帅哥,yi和yj为美女,假设给定的完美

[HDU1512]:Monkey King

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6274    Accepted Submission(s): 2678 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they e

ZOJ 2334 HDU 1512 Monkey King

题意: 猴子们打架  认识的猴子不会打架  两只猴子打完以后就认识了  A认识B B认识C A也认识C  每次打架由两伙猴子进行  分别选出自己的最高战斗力  在战斗之后两只猴子战斗力减半  给出m次打架  输出打架后这一伙猴子里的最强战斗力 思路: 判断两只猴子是不是一伙的  用到并查集 快速找出一伙猴子中的最强战斗力用到堆  但打完架两伙猴子合并时堆需要nlogn复杂度  因此用左偏树代替堆 代码: #include<cstdio> #include<cstring> #inc

POJ 1904 King&#39;s Quest 强连通分量+二分图增广判定

http://www.cnblogs.com/zxndgv/archive/2011/08/06/2129333.html 这位神说的很好 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <string> #include <stack> #include <ve

【HDU 1512】Monkey King

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3423    Accepted Submission(s): 1479 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they