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. EF 记录执行的sql语句

    最近做了个中等的项目,数据不会很多,开发时间比较紧迫,所以用了EF的框架. 在使用过程中,发现有时候执行的结果不如预期,想看看执行的sql语句为何,遍查找资料,在网上找到了相关辅助类,拿来使用,部署到 ...

  2. 安卓下对SD卡文件的读写

    为SD下的操作文件,封装了一些类: package ujs.javawritedata; import java.io.File; import java.io.FileInputStream; im ...

  3. 在CentOS上源码安装Nginx

    总步骤: wget http://nginx.org/download/nginx-1.10.1.tar.gz tar -xvf nginx-1.10.1.tar.gz cd nginx-1.10.1 ...

  4. 初学Vue.js(2.x版本)

    首先肯定是打开官网查看文档了,没想到我太高估了自己,看的我头晕也不知道到底说了个啥.没办法,只能另寻他法,好在有菜鸟教程.然而我还是想多了,不稀饭一点点看下去,只想快点明白它到底说了个啥.嗯,找来找去 ...

  5. 织梦DEDECMS会员中心发布文章修改提示"数据校验不对,程序返回"

    一.文件:member/inc/inc_archives_function.php 找到函数 function PrintAutoFieldsEdit(&$fieldset, &$fi ...

  6. Android中当item数量超过一定大小RecyclerView高度固定

    Android中当item数量超过一定大小时,将RecyclerView高度固定 方法1 直接通过LayoutParams来设定相应高度 ViewGroup.LayoutParams lp = rv. ...

  7. requests模块下载视频 显示进度和网速

    requests 下载视频 import os,time import requests def downloadFile(name, url): headers = {'Proxy-Connecti ...

  8. equals()方法详解

    Java语言中equals()方法的使用可以说比较的频繁,但是如果轻视equals()方法,一些意想不到的错误就会产生.哈哈,说的有点严重了~ 先谈谈equals()方法的出身.equals()方法在 ...

  9. 如何选择Web开发框架

    下面先来看看为什么要使用Web开发框架一 使用框架的必然性框架,即framework.其实就是某种应用的半成品,把不同应用程序中有共性的一些东西抽取出来,做成一个半成品程序,这样的半成品就是所谓的程序 ...

  10. This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms while caching

    今天运行自己的网站时报了这样一个错误,很是纳闷,这个网站运行了这么久,怎么报这个错呢,原来是做缓存的时候用到了基于windows平台的加密算法.解决方法如下: 删除注册表下的这个节点即可.删除HKEY ...