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.
输入输出样例
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
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][];
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][]=merge(ch[x][],y);
f[ch[x][]]=x;
if(dis[ch[x][]]>dis[ch[x][]]) swap(ch[x][],ch[x][]);
dis[x]=dis[ch[x][]]+; 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])>>);
if(val[fa]>val[fb]) val[fa]>>=;
else val[fb]>>=; f[ch[fa][]]=f[ch[fa][]]=;
nowx=merge(ch[fa][],ch[fa][]);
ch[fa][]=ch[fa][]=;
nowx=merge(nowx,fa); f[ch[fb][]]=f[ch[fb][]]=;
nowy=merge(ch[fb][],ch[fb][]);
ch[fb][]=ch[fb][]=;
nowy=merge(nowy,fb); merge(nowx,nowy);
} int main(){
while(scanf("%d",&n)==&&n){
memset(f,,sizeof(f));
memset(dis,,sizeof(dis));
memset(ch,,sizeof(ch));
for(int i=;i<=n;i++) scanf("%d",val+i);
scanf("%d",&m);
int uu,vv;
while(m--){
scanf("%d%d",&uu,&vv);
work(uu,vv);
}
} return ;
}
POJ ???? Monkey King的更多相关文章
- ZOJ 2334 Monkey King
并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子 Monkey King ...
- 数据结构(左偏树):HDU 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- P1456 Monkey King
题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- Monkey King(左偏树 可并堆)
我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...
- 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- The Monkey King(hdu5201)
The Monkey King Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- POJ 1904 King's Quest tarjan
King's Quest 题目连接: http://poj.org/problem?id=1904 Description Once upon a time there lived a king an ...
- Poj 1904 King's Quest 强连通分量
题目链接: http://poj.org/problem?id=1904 题意: 有n个王子和n个公主,王子只能娶自己心仪的公主(一个王子可能会有多个心仪的公主),现已给出一个完美匹配,问每个王子都可 ...
随机推荐
- 【洛谷 P1707】 刷题比赛 (矩阵加速)
题目连接 很久没写矩阵加速了,复习一下,没想到是一道小毒瘤题. 状态矩阵\(a[k],b[k],c[k],a[k+1],b[k+1],c[k+1],k,k^2,w^k,z^k,1\) 转移矩阵 0, ...
- TDD随想录
TDD随想录 谨以本文献给TDD的开创者与传播者 本文纯属个人经历,如有雷同纯属巧合 我从不觉得自己是一个好的程序员,甚至可能连合格都谈不上,不过在内心深处我却渴望着在编程这件事上获得成功. 可惜每次 ...
- 生成应用的快捷方式action,权限
action:"com.android.launcher.action.INSTALL_SHORTCUT" 权限:com.android.launcher.permission.I ...
- Java 中的成员内部类
内部类中最常见的就是成员内部类,也称为普通内部类.我们来看如下代码: 运行结果为: 从上面的代码中我们可以看到,成员内部类的使用方法: 1. Inner 类定义在 Outer 类的内部,相当于 Out ...
- perl6中函数参数(1)
sub F($number is copy){ $number++; say $number; } F(); #下面是错误的 sub F($number){ $number++; say $numbe ...
- linux pthread【转】
转自:http://www.cnblogs.com/alanhu/articles/4748943.html Posix线程编程指南(1) 内容: 一. 线程创建 二.线程取消 关于作者 线程创 ...
- 自动化测试===Macaca环境搭建,自我总结
安装jdk 安装安卓sdk(打开sdk的时候出现问题linux===启动sdk manager下载配置sdk的时候报错的解决办法) 安装gradle,配置环境变量(MACACA===gradle下载和 ...
- GLIBCXX_3.4.9' not found - 解决办法
GLIBCXX_3.4.9' not found - 解决办法 http://blog.csdn.net/u012425536/article/details/26559653 https://koj ...
- 【bzoj4552】排序
二分一个值,然后线段树上模拟. #include<bits/stdc++.h> #define lson (o<<1) #define rson (o<<1|1) ...
- [New learn]GCD其他方法的使用
https://github.com/xufeng79x/GCDDemo 1.简介 在前面的两篇博文中我介绍了GCD的一般使用方法和死锁的分析调查.本博文中继续讲解GCD的其他比较常用的几个使用方法. ...