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. 【UML】活动图Activity diagram(转)

    前言 在UML状态图的总结中说道,活动图和状态图是紧密相关的.它与流程图也有很多相似的地方. 定义 活动图是状态图的一种特殊形式.其中所有或多数状态都是活动状态,而且所有或多数转移都在源状态中的活动完 ...

  2. Codeforces Round #319 (Div. 2) B Modulo Sum (dp,鸽巢)

    直接O(n*m)的dp也可以直接跑过. 因为上最多跑到m就终止了,因为前缀sum[i]取余数,i = 0,1,2,3...,m,有m+1个余数,m的余数只有m种必然有两个相同. #include< ...

  3. CF Gym 100187B A Lot of Joy (古典概型)

    题意:给两个一样的只含有26个小写字母的字符串,然后两个分别做一下排列,问如果对应位置的字母相等那么就愉悦值就加一,问愉悦值的期望是多少? 题解:只考虑两个序列相对的位置,那么就相当于固定一个位置,另 ...

  4. 【转】JavaScript 节点操作 以及DOMDocument属性和方法

    最近发现DOMDocument对象很重要,还有XMLHTTP也很重要 注意大小写一定不能弄错. 属性: 1Attributes 存储节点的属性列表(只读) 2childNodes 存储节点的子节点列表 ...

  5. 《毛毛虫组》【Alpha】Scrum meeting 1

    第一天 日期:2019/6/14 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: (1)根据数据库设计时的E-R图将创建的表进行检查确保功能的正确实现. (2)进行公共类的设计,设计出程 ...

  6. “CTL_CODE”未定义

    C4013 “CTL_CODE”未定义:假设外部返回 int 要加入 #include<winioctl.h> 并且要放在#include<windows.h>的后面

  7. iOS开发-动画总结

    一.简介 IOS 动画主要是指Core Animation框架.官方使用文档地址为:Core Animation Guide.Core Animation是IOS和OS X平台上负责图形渲染与动画的基 ...

  8. 配置淘宝镜像,不使用怪异的cnpm

    npm config set registry https://registry.npm.taobao.org --global npm config set disturl https://npm. ...

  9. vuePress的使用

    今天来玩一玩vuePress的使用,用markdown来编辑一个页面网站,这里谈论到了简单使用,细节可以去官网上去查看 开始安装 项目依赖 // package.json { "name&q ...

  10. 【转】MFC编辑框自动换行,垂直滚动条自动下移

    1.新建一个编辑框控件(Edit Control),将其多行(Multiline)前面打勾(属性设置为True),Auto HScroll前面的勾去掉(属性设置False),这样就可以实现每一行填满后 ...