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 ;
}

最新文章

  1. MVC中如何跳过对模型中某个属性的验证
  2. C/C++易错点
  3. 3DShader之立方体环境映射(cubic environment mapping)
  4. PHP-微信公众平台开发-接收用户输入消息类型并响应
  5. macbook pro 突破校园网inode客户端限制分享网络
  6. JS数组filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()实例
  7. com.alibaba.druid.sql.parser.ParserException: syntax error, QUES %, pos 80 like报错解决
  8. ZOJ-1456 Minimum Transport Cost---Floyd变形+路径输出字典序最小
  9. Spring 实现事务的三种方式
  10. 教女朋友写第一个php
  11. SharePoint Framework 企业向导(一)
  12. Redis数据结构深入解析
  13. Redis学习笔记11--Redis分布式
  14. 判断当前Selection是否为prefab
  15. Java中final修饰参数的作用
  16. ecmall程序结构图与数据库表分析
  17. 网络安装CentOS6.4
  18. Ngnix 安装常见错误的处理
  19. 【BZOJ1132】[POI2008]Tro 几何
  20. ssemble JavaBeans components into an application without having to write any code

热门文章

  1. Tensorflow版Faster RCNN源码解析(TFFRCNN) (1) VGGnet_test.py
  2. Spring的七种事务传播机制
  3. feign hystrix加仪表盘
  4. Seven Deadly Sins: Gluttony, Greed, Sloth, Wrath, Pride, Lust, and Envy.
  5. Protocol Buffer学习教程之编译器与类文件(三)
  6. C# 向服务器上传文件(客服端winform、服务端web)
  7. dataset datatable datacolums datarow
  8. nginx只允许域名访问网址,禁止ip访问
  9. code Gym 100500D T-shirts(暴力)
  10. Object Modeling