BZOJ 1455 罗马游戏 ——左偏树
【题目分析】
左偏树的模板题目,大概就是尽量维护树的深度保持平衡,以及尽可能的快速合并的一种堆。
感觉和启发式合并基本相同。
其实并没有快很多。
本人的左偏树代码自带大常数,借鉴请慎重
【代码】
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <cmath>
- #include <set>
- #include <map>
- #include <string>
- #include <algorithm>
- #include <vector>
- #include <iostream>
- #include <queue>
- using namespace std;
- #define maxn 1000005
- int read()
- {
- int x=0,f=1; char ch=getchar();
- while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
- while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
- return x*f;
- }
- int fa[maxn],v[maxn];
- int n,q,l[maxn],r[maxn],die[maxn],d[maxn];
- char opt[11];
- int gf(int k)
- {
- if (fa[k]==k) return k;
- else return fa[k]=gf(fa[k]);
- }
- int merge(int x,int y)
- {
- if (!(x*y)) return x+y;
- if (v[x]>v[y]) swap(x,y);
- r[x]=merge(r[x],y);
- if (d[l[x]]<d[r[x]]) swap(l[x],r[x]);
- d[x]=d[r[x]]+1;
- return x;
- }
- int main()
- {
- n=read();
- for (int i=1;i<=n;++i) v[i]=read(),fa[i]=i;
- q=read();
- while (q--)
- {
- scanf("%s",opt);
- if (opt[0]=='M')
- {
- int x=read(),y=read();
- if (die[x]||die[y]) continue;
- int fx=gf(x),fy=gf(y);
- if (fx!=fy) fa[fx]=fa[fy]=merge(fx,fy);
- }
- else
- {
- int x=read();
- if (die[x])
- {
- printf("0\n");
- continue;
- }
- else
- {
- int fx=gf(x);
- die[fx]=1;
- printf("%d\n",v[fx]);
- int tmp=merge(l[fx],r[fx]);
- fa[fx]=fa[tmp]=tmp;
- }
- }
- }
- }
BZOJ 1455 罗马游戏 ——左偏树的更多相关文章
- bzoj 1455: 罗马游戏 左偏树+并查集
1455: 罗马游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 668 Solved: 247[Submit][Status] Descriptio ...
- BZOJ 1455 罗马游戏 左偏树
题目大意:给定n个点,每一个点有一个权值,提供两种操作: 1.将两个点所在集合合并 2.将一个点所在集合的最小的点删除并输出权值 非常裸的可并堆 n<=100W 启示式合并不用想了 左偏树就是快 ...
- 1455: 罗马游戏[左偏树or可并堆]
1455: 罗马游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1861 Solved: 798[Submit][Status][Discuss] ...
- BZOJ1455 罗马游戏 左偏树 可并堆
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1455 题意概括 n个人,2种操作. 一种是合并两个人团,一种是杀死某一个人团的最弱的人. 题解 左 ...
- [BZOJ1455]罗马游戏 左偏树+并查集
1455: 罗马游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 2285 Solved: 994[Submit][Status][Discuss] ...
- BZOJ 1455: 罗马游戏 [可并堆]
1455: 罗马游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1715 Solved: 718[Submit][Status][Discuss] ...
- bzoj 1455: 罗马游戏
1455: 罗马游戏 Time Limit: 5 Sec Memory Limit: 64 MB Description 罗马皇帝很喜欢玩杀人游戏. 他的军队里面有n个人,每个人都是一个独立的团.最 ...
- BZOJ 1455: 罗马游戏( 配对堆 + 并查集 )
可并堆水题 --------------------------------------------------------- #include<bits/stdc++.h> usin ...
- bzoj 4003: 城池攻占 左偏树
题目大意 http://www.lydsy.com/JudgeOnline/problem.php?id=4003 题解 一开始看漏条件了 题目保证当占领城池可以使攻击力乘上\(v_i\)时,一定有\ ...
随机推荐
- mybatis一个怪异的问题: Invalid bound statement not found
ssm中报一下错误: invalid bound statement (not found): me.tspace.pm.dao.userdao.getuser at org.apache.ib ...
- python更新后yum问题
How to switch between Python versions on Fedora Linux Currently, the default python version on Fedor ...
- Android热修复AndFix
热修复主要用来修复代码.修复bug.添加独立的功能,他的原理主要是操作PathClassLoader.DexClassLoader. PathClassLoader是类加载器,DexClassLoad ...
- spring aop的使用
使用上次整合的s2sh工程,加入aop的功能.aop(面向切面编程)是一种对oop的完善,比如在进行数据库操作的时候执行方法前或者后加入一条日志记录,使用一个额外的类去做日志操作,让dao类专注地做它 ...
- 搭建haproxy
1:下载haproxy wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz2:解压,编译,安装 tar zxf hapr ...
- css清楚浮动的方法
- zendstudio快捷键收录
360截屏快捷键:ctrl+shift+x zendstudio:注释代码:ctrl+shift+/ 删除光标所在行:ctrl+D 复制当前行:ctrl+alt+↓ 上下行互换:alt+↑/↓ 代码格 ...
- poj1062 昂贵的聘礼
Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...
- 【html】学习记录-拖放(drag and drop)
目的:实现拖动目标并放置到指定区域. 使元素可拖动,涉及到元素的全局属性draggable <img draggable="true" /> 即img元素设置为可拖 ...
- Android高手速成--第四部分 开发工具及测试工具
第四部分 开发工具及测试工具 主要介绍和Android开发工具和测试工具相关的开源项目. 一.开发效率工具 Json2Java根据JSon数据自动生成对应的Java实体类,还支持Parcel.Gson ...