Monkey King

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6667    Accepted Submission(s):
2858

Problem Description

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.

 

Input

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.

 

Output

For each of the conflict, output -1 if the two monkeys
know each other, otherwise output the strongness value of the strongest monkey
in all friends of them after the duel.
 

Sample Input

5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
 

Sample Output

8 5 5 -1 10
 

分析

左偏树,对于每次操作,取出最大的元素,将它从删除(即合并他的两个子树),除2后在加入进去。然后操作完成后,将两棵树合并即可。

code

 #include<cstdio>
#include<algorithm>
#include<cstring> using namespace std; const int N = ; int val[N],dis[N],ls[N],rs[N],fa[N]; inline void read(int &x) {
x = ;int f = ;char ch = getchar();
for (; ch<''||ch>''; ch = getchar()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = getchar()) x = x * + ch - '';
x = x * f;
}
int merge(int x,int y) {
if (!x || !y) return x + y;
if (val[x]<val[y]||(val[x]==val[y]&&x<y)) swap(x,y);
rs[x] = merge(rs[x],y);
fa[rs[x]] = x;
if (dis[ls[x]] < dis[rs[x]]) swap(rs[x],ls[x]);
if (rs[x]) dis[x] = dis[rs[x]] + ;
else dis[x] = ;
return x;
}
inline int find(int x) {
if (x==fa[x]) return x;
return fa[x] = find(fa[x]);
}
inline int work(int x) {
fa[ls[x]] = ls[x];fa[rs[x]] = rs[x]; //-
int t = merge(ls[x],rs[x]);
ls[x] = rs[x] = ; //-
val[x] /= ;
return merge(x,t);
}
int main() {
int m,n,a,b;
while (~scanf("%d",&n)) {
memset(rs,,sizeof(rs));
memset(ls,,sizeof(ls));
memset(dis,,sizeof(dis));
for (int i=; i<=n; ++i) read(val[i]),fa[i] = i;
read(m);
while (m--) {
read(a),read(b);
int x = find(a),y = find(b);
if (x == y) puts("-1");
else printf("%d\n",val[merge(work(x),work(y))]);
}
}
return ;
}

1512 Monkey King的更多相关文章

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

    Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  2. hdu 1512 Monkey King 左偏树

    题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...

  3. 【HDOJ】1512 Monkey King

    左偏树+并查集.左偏树就是可合并二叉堆. /* 1512 */ #include <iostream> #include <string> #include <map&g ...

  4. HDU 1512 Monkey King(左偏树+并查集)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1512 [题目大意] 现在有 一群互不认识的猴子,每个猴子有一个能力值,每次选择两个猴子,挑出他们所 ...

  5. HDU 1512 Monkey King(左偏树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意: 有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了 ...

  6. HDU 1512 Monkey King(左偏树)

    Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each does thi ...

  7. hdu 1512 Monkey King —— 左偏树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...

  8. HDU 1512 Monkey King ——左偏树

    [题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...

  9. HDU 1512 Monkey King

    左偏树.我是ziliuziliu,我是最强的 #include<iostream> #include<cstdio> #include<cstring> #incl ...

随机推荐

  1. windows环境下memcache配置方法 详细篇

    将memcache服务器安装包解压到C:\memcached文件夹后,使用cmd命令窗口安装. 1>开始>运行:CMD(确定) 2>cd C:\memcached(回车) 3> ...

  2. P3290 寻找第K大数

    描述 寻找第K大数 N个小朋友在一起做游戏.每个小朋友在自己的硬纸板上写一个数,然后同时举起来.接着,小y老师提一个问题,看哪个小朋友先抢答出来.问题是:在这N个数中,第K大的是哪个数?请你编程完成. ...

  3. 超文本传输协议(HTTP)

    超文本传输协议(HyperText Transfer Protocol,Http)是从服务器传输数据到客户端的传输协议. HTTP协议的主要特点可概括如下: 1.支持客户/服务器模式. 2.简单快速: ...

  4. SpringMVC注解方式与文件上传

    目录: springmvc的注解方式 文件上传(上传图片,并显示) 一.注解 在类前面加上@Controller 表示该类是一个控制器在方法handleRequest 前面加上 @RequestMap ...

  5. tcp的三次连接握手和四次释放握手

    http://blog.csdn.net/whuslei/article/details/6667471/ 这篇博客讲的很清楚. 下面我简单说明一下:三次连接握手,首先client发送请求报文,然后服 ...

  6. bootstrap fileinput 上传文件

    最近用到文件上传功能, 说实话:以前遇到过一次,COPY了别人的代码 结束! 这次又要用,可是看到别人很酷的文件上传功能,心痒了! 好吧.简单的办法,找控件: bootstrap fileinput ...

  7. Brackets安装angularjs插件

    Brackets是Adobe公司研发的一款开源WEB前端开发框架,界面清爽简约,代码提示功能比较强大,而且支持第三方插件,其提供的插件库中有大量的对Brackets感兴趣的开发人员所开发的插件,使用者 ...

  8. 动态加载sd卡或者手机内置存储卡的so库

    package com.wsc.utils; import android.content.Context; import com.wsc.common.Entrance; import com.ws ...

  9. ssh连接github连不上

    连接github报端口22连接不上: 输入命令展示出ssh_config内容后: vim /etc/ssh/ssh_config 或者使用open /etc/ssh/ssh_config命令在文本编辑 ...

  10. VB SMTP用户验证发送mail

    转自 http://www.jishuzh.com/program/vb-smtp%E7%94%A8%E6%88%B7%E9%AA%8C%E8%AF%81%E5%8F%91%E9%80%81mail. ...