题目描述

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.

一开始有n只孤独的猴子,然后他们要打m次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识o(∩_∩)o )。问每次打完架之后那俩猴子最牛叉的朋友战斗力还有多少,若朋友打架就输出-1.

输入输出格式

输入格式:

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.

输入输出样例

输入样例#1:

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

输出样例#1:

8
5
5
-1
10

说明

题目可能有多组数据

思路:左偏堆

代码实现

 1 #include<cstdio>
 2 #include<cstring>
 3 const int maxn=1e5+10;
 4 int n,m;
 5 int a,b,c,d;
 6 int s[maxn],f[maxn],t[maxn],l[maxn],r[maxn];
 7 inline int swap_(int&x,int&y){x^=y,y^=x,x^=y;}
 8 int ff(int x){return f[x]==x?x:f[x]=ff(f[x]);}
 9 int merger(int x,int y){
10     if(!x) return y;
11     if(!y) return x;
12     if(s[x]<s[y]) swap_(x,y);
13     r[x]=merger(r[x],y);
14     if(t[r[x]]>t[l[x]]) swap_(r[x],l[x]);
15     t[x]=t[r[x]]+1;
16     return x;
17 }
18 int main(){
19     while(scanf("%d",&n)!=EOF){
20         memset(s,0,sizeof(s));
21         memset(f,0,sizeof(f));
22         memset(t,0,sizeof(t));
23         memset(l,0,sizeof(l));
24         memset(r,0,sizeof(r));
25         for(int i=1;i<=n;i++) scanf("%d",&s[i]),f[i]=i;
26         scanf("%d",&m);
27         while(m--){
28             scanf("%d%d",&a,&b);
29             a=ff(a),b=ff(b);
30             if(a==b) puts("-1");
31             else{
32                 s[a]/=2,s[b]/=2;
33                 c=f[l[a]]=f[r[a]]=merger(l[a],r[a]);
34                 d=f[l[b]]=f[r[b]]=merger(l[b],r[b]);
35                 l[a]=r[a]=l[b]=r[b]=0;
36                 a=f[a]=f[c]=merger(a,c);
37                 b=f[b]=f[d]=merger(b,d);
38                 f[a]=f[b]=merger(a,b);
39                 printf("%d\n",s[f[a]]);
40             }
41         }
42     }
43     return 0;
44 }

我为什么只写了合并操作,才不是因为lazy(mmjs),只是为了更好地理解左偏树的性质。

转载于:https://www.cnblogs.com/J-william/p/6970251.html

Monkey King相关推荐

  1. 有趣的排序算法——Monkey King排序 详细介绍

    <关于某位小蒟蒻在机房划水没事干于是瞎搞的那档事> 前言 排序算法在题目中经常需要用到,在程序中,我们一般打的是快排,归并,堆排等高效率排序,更有甚者会直接用sort排序,而今天,我要介绍 ...

  2. Monkey King(左偏树 可并堆)

    我们知道如果要我们给一个序列排序,按照某种大小顺序关系,我们很容易想到优先队列,的确很方便,但是优先队列也有解决不了的问题,当题目要求你把两个优先队列合并的时候,这就实现不了了 优先队列只有插入 删除 ...

  3. 洛谷1456 Monkey King

    题目:Monkey King 思路:左偏树.对于每次操作 x y ,先让x,y等于各自朋友中最大的那个,即x=find(x),y=find(y),然后再分别把x,y删除,修改权值后重新加入堆中. 代码 ...

  4. 洛谷P1456 Monkey King

    题目描述 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in ...

  5. 【洛谷】P1456 Monkey King

    题目地址: https://www.luogu.com.cn/problem/P1456 题面翻译: 题目描述: 曾经在一个森林中居住着NNN只好斗的猴子.在最初他们我行我素,互不认识.但是猴子们不能 ...

  6. Monkey King - 左偏树

    题目描述 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in ...

  7. 洛谷 P1456 Monkey King 题解

    标签:左偏树&并查集 思路来源: Q1:为什么用并查集? A1:因为要判断两队猴子是否认识,在此处使用并查集可以便于维护两队猴子间的关系. Q2:为什么用堆? A2:因为每次对战都需要查找当前 ...

  8. 【HDU 1512】Monkey King

    [题目] 传送门 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, t ...

  9. zoj 2334 Monkey King

    左偏树加并查集 一开始,对于每一个monkey建立一个单节点的左偏树. 对于每一次duel.,将对应的两只monkey的左偏树根节点的值减半,即最强壮的两只monkey强壮值减半,更新维护两个对应的左 ...

最新文章

  1. 世界顶级摄影作品,构图的最高境界!
  2. spss相关性分析看结果_利用spss做Pearson相关性分析步骤详解
  3. 让网站具有生命与活力不仅仅是说说而已
  4. scp时候出现ssh Connection refused的解决方案
  5. Python的第一个程序 Hello world
  6. 常用数学特殊符号,复制到word中使用
  7. 第七章:【UCHome二次开发】模板语法说明
  8. rpg制作大师2003_RPG制作大师MV 我们一起做游戏(十五)
  9. 小程序map地图多点定位
  10. Python整理PEER所下载的地震波源数据——提取地震波至txt+生成地震波反应谱
  11. Java多个PDF文件合并成一个PDF文件
  12. 解决扫码枪中文输入法冲突问题
  13. showdown让你的网站支持Markdown和代码块highlight
  14. 专访百度AI交互设计院院长关岱松:感知类学科的尽头都是心理学 | 甲子光年
  15. Axure 设计App界面
  16. 基于python-flask制作的论坛
  17. 民间秘术——镇鬼送神
  18. matlab相关,来自一个初学者的收藏
  19. VS2017登陆失败:我们无法刷新此账户的凭证、我们无法添加此账户发送请求时出错、评估期已结束,请登录以解除产品锁定
  20. word转图片,pdf转图片,doc转图片,docx转图片

热门文章

  1. vue框架+cesium项目怎么才能运行成功
  2. 计算机网络拓扑结构方案总结,计算机网络拓扑结构总结
  3. java抓取网页数据_简易数据分析 10 | Web Scraper 翻页——抓取滚动加载类型网页...
  4. 2接口详解_冯博琴微型计算机原理与接口技术第3版答案资料配套题库名校考研真题课后习题章节题库模拟试题...
  5. html5历史纪录管理,HTML 5 新增方法以及历史管理
  6. p坚持csma协议 仿真‘_巧家快速推进移民生产安置人口界定和协议签订工作
  7. java jstl标签库_JSTL标签库
  8. 4K 海思 联咏 芯片_强悍芯片,重装来袭-海美迪H7 Plus旗舰4K电视盒子体验
  9. html 分页 惰性加载,懒加载实现的分页网站footer自适应
  10. 移动端html右滑空白,BootStrap.css 在手机端滑动时右侧出现空白的原因及解决办法...