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个公主,王子只能娶自己心仪的公主(一个王子可能会有多个心仪的公主),现已给出一个完美匹配,问每个王子都可 ...
随机推荐
- 【洛谷 P3165】 [CQOI2014]排序机械臂 (Splay)
题目链接 debug了\(N\)天没debug出来,原来是找后继的时候没有pushdown... 众所周知,,Splay中每个编号对应的节点的值是永远不会变的,因为所有旋转.翻转操作改变的都是父节点和 ...
- Fetch-新一代Ajax API
AJAX半遮半掩的底层API是饱受诟病的一件事情. XMLHttpRequest 并不是专为Ajax而设计的. 虽然各种框架对 XHR 的封装已经足够好用, 但我们可以做得更好. window.fet ...
- Python的异常处理机制 -- (转)
当你的程序中出现异常情况时就需要异常处理.比如当你打开一个不存在的文件时.当你的程序中有一些无效的语句时,Python会提示你有错误存在. 下面是一个拼写错误的例子,print写成了Print.Pyt ...
- hdu 1879 继续畅通工程 (并查集+最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1879 继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) ...
- perl6中函数参数(1)
sub F($number is copy){ $number++; say $number; } F(); #下面是错误的 sub F($number){ $number++; say $numbe ...
- 关于$->aaa->bbb();的困惑
第21行为什么可以调用test类的aa方法呢? 答:因为前一行(20)其已经被实例化了.所以现在的$this->obj其实可以相当于是一个对象. 20行和21行也可以写成如下 $xxoo = n ...
- 日常开发技巧:使用notify-send发送通知
背景 在终端执行一些需要较长时间的命令时,会切换到别的界面.但为了知道是否执行完成,需要时不时地切换过去看一眼.很麻烦. 解决方式 为了减少这种麻烦,可以使用notify-send,发送桌面通知.no ...
- ACdream 1157 Segments CDQ分治
题目链接:https://vjudge.net/problem/ACdream-1157 题意: Problem Description 由3钟类型操作: 1)D L R(1 <= L < ...
- HEER-Easing Embedding Learning by Comprehensive Transcription of Heterogeneous Information Networks
来源:KDD 2018 原文:HEER code:https://github.com/GentleZhu/HEER 注: 若有错误,欢迎指正 这篇KDD’18的文章,没有按照常规的方法将所有的n ...
- MapReduce程序的几种提交运行模式
本地模型运行 1/在windows的eclipse里面直接运行main方法,就会将job提交给本地执行器localjobrunner执行 ----输入输出数据可以放在本地路径下(c:/wc/ ...