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个公主,王子只能娶自己心仪的公主(一个王子可能会有多个心仪的公主),现已给出一个完美匹配,问每个王子都可 ...
随机推荐
- new操作符(翻译自mozilla.org)
翻译自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new new操作符可以实例化一个用户自 ...
- SpringCloud Feign重试详解
摘要: 今天在生产环境发生了数据库进程卡死的现象,除了sql因为全量更新,没加索引的原因,最主要还是我们的接口的服务器端接口出现问题了.忽视了更新接口的幂等性,以及调用方feign client的重试 ...
- css文本垂直水平居中
一.单行文本居中 .content{ height:100px; line-height:100px; text-align:center; border:1px solid red; } 效果图 二 ...
- HTTP和HTTPS详解。
一,HTTP和HTTPS基本概念 深入学习某个东西时,我们先来从维基百科上看看它俩的概念. HTTP:超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一 ...
- Opencv模块功能介绍
本文为原创作品,转载请注明出处 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing/ 站在巨 ...
- $(document).ready 和 window.onload 的区别
1.相同点 两者都用于在网页加载完后执行相应代码块. 2.不同点 window.onload 在创建完 DOM 树后,所有外部资源(图片.Flash 动画等)加载完成,且整个页面在浏览器窗口中显示完毕 ...
- python基础===* 解包,格式化输出和print的一点知识
python3中的特性: >>> name = "botoo" >>> print(f"my name is {name}" ...
- c语言简单实现telnet客户端
c语言简单实现telnet客户端 http://blog.csdn.net/haiwenchen/article/details/69944118
- 【玲珑杯Round17】xjb总结
zcy真是垃圾,啥都不会的那种. 菜的不行. 这场手速上了三题,然后各种E被卡…… 日个吗居然E不开栈,傻逼吧 有毒吧 来看题: A.sqc给的我的神奇公式,gtmd居然能A? #include< ...
- mac 使用清除废纸篓或彻底删除某文件 附加: smb afp ftp NAS 访问服务器相关
mac 使用清除废纸篓或彻底删除某文件 附加: smb afp ftp NAS 访问服务器相关 mac 下删除文件方法: 1.使用 cleanmymac 使用 cleamymac 的清理 和 逐个 ...