ZOJ2334 Monkey King 左偏树
用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用。
左偏树代码的核心也是两棵树的合并!
代码有些细节需要注意。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=200000;
struct monkey{
int num;int group;int strong;int ls; int rs;int dist;
};
int n,m,tot;
monkey mon[maxn];
int find(int x)
{
if(mon[x].group==x||mon[x].group==0)return x;
else return mon[x].group=find(mon[x].group);
} int Top(int x)
{
return find(x);
} int Merge(int x,int y)
{
if(x==y)return x;
if(!x)return mon[y].group=y;
if(!y)return mon[x].group=x;
if(mon[x].strong<mon[y].strong)swap(x,y); mon[x].rs=Merge(mon[x].rs,y);
mon[mon[x].rs].group=x;
if(mon[mon[x].ls].dist<mon[mon[x].rs].dist)swap(mon[x].ls,mon[x].rs);
mon[x].dist=mon[mon[x].rs].dist+1;
return mon[x].group=x;
}
int Pop(int x)
{ mon[x].group=Merge(mon[x].ls,mon[x].rs);
mon[x].ls=mon[x].rs=mon[x].dist=0;
} void Insert(int x,int y)
{
Merge(find(x),y);
}
int main()
{freopen("t.txt","r",stdin);
//freopen("1.txt","w",stdout);
tot=0;
while(scanf("%d",&n)!=EOF)
{
tot++;
memset(mon,0,sizeof(mon));
for(int i=1;i<=n;i++)
{ mon[i].num=mon[i].group=i;mon[i].ls=mon[i].rs=mon[i].dist=0;
scanf("%d",&mon[i].strong);
}
scanf("%d",&m);
int a,b;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
a=find(a);b=find(b);
if(a==b){printf("-1\n");continue;}
mon[a].strong/=2;mon[b].strong/=2;
Pop(a);
Pop(b);
Insert(find(a),a);
Insert(find(b),b); printf("%d\n",max(mon[find(a)].strong,mon[find(b)].strong));
Merge(find(a),find(b));
}
}
return 0;
}
ZOJ2334 Monkey King 左偏树的更多相关文章
- HDU1512 ZOJ2334 Monkey King 左偏树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...
- hdu 1512 Monkey King 左偏树
题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...
- zoj 2334 Monkey King/左偏树+并查集
原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...
- HDU 1512 Monkey King (左偏树+并查集)
题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...
- hdu 1512 Monkey King —— 左偏树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...
- hdu1512 Monkey King(左偏树 + 并查集)
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...
- LuoguP1456 Monkey King (左偏树)
struct LeftTree{ int l,r,val,dis; }t[N]; int fa[N]; inline int Find(int x){ return x == fa[x] ? x : ...
- HDU 1512 Monkey King ——左偏树
[题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...
- HDU 1512 Monkey King(左偏堆)
爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...
随机推荐
- Java基础(十)--static关键字
static关键字通常应用在字段.方法.静态块,还有冷门一点的内容:静态内部类.静态导入 static字段: static字段也就是静态变量,是属于类的,被所有的对象所共享,只有当类初次加载的时候保存 ...
- 09Oracle Database 数据表数据插入,更新,删除
Oracle Database 数据表数据插入,更新,删除 插入数据 Insert into table_name(column) values(values); insert into studen ...
- 可以通过dict[key]获得dict[value]
dict={key:value,key2:value2} print (dict[key] ) 得到的是 dict[value] # 软文预存接口,通过key来预览未保存的软文,联查商品.kol ...
- 什么是restful
- shell输出颜色、printf输出颜色
1.echo开启彩色输出: -e 开启echo中的转义: \e或者\033来输出Esc颜色: 恢复默认颜色为:\e[0m; 命令格式: echo -e "\e[字背景颜色:文字颜色m字符串\ ...
- HYSBZ - 1050(旅行comf 并查集Java实现)
HYSBZ - 1050(旅行comf Java实现) 原题地址 解法:枚举每一条边,对于这条边,我们需要找到集合中和其值相差最小的最大边,这个集合是指与包括i边在内的ST联通集.对于这一要求,我们只 ...
- Spring常用注解总结 hibernate注解
1.@Resource和@Autowired @Resource和@Autowired功能一样在容器查找匹配的Bean @Autowired默认按照byType方式进行bean匹配,@Resource ...
- Butterfly
Butterfly 时间限制:C/C++ 2秒,其他语言4秒空间限制:C/C++ 131072K,其他语言262144K64bit IO Format: %lld 题目描述 给定一个n*m的矩阵,矩阵 ...
- hdu 3804树链剖分+离线操作
/* 树链刨分+离线操作 题意:给你一棵树,和询问x,y 从节点x--节点1的小于等于y的最大值. 解:先建一个空树,将树的边权值从小到大排序,将询问y按从小到大排序 对于每次询问y将小于等于y的边权 ...
- sharepoint第三方程序认证尝试失败记录
本来想用REST服务开发第三方的插件的,下面的文章说明REST API是非常好用的 https://technet.microsoft.com/zh-cn/library/jj164022(v=off ...