hdu 1512 Monkey King 左偏树
题目链接:HDU - 1512
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.
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.
/*左偏树*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn = +; int father[maxn];
struct node
{
int l,r;
int dis;
int strong;
}LTree[maxn];
int Find(int x)
{
if (father[x]==x) return x;
return father[x]=Find(father[x]);
}
int Merge(int x,int y)
{ //返回合并后的根
if (x==) return y;
if (y==) return x;
if (LTree[x].strong < LTree[y].strong) //大顶堆
swap(x,y);
LTree[x].r = Merge(LTree[x].r,y); //递归合并右子树和Y
int l = LTree[x].l , r = LTree[x].r;
father[r] = x; //更新T右子树的根
if (LTree[l].dis < LTree[r].dis) //维护堆性质
swap(LTree[x].l,LTree[x].r);
if (LTree[x].r == ) //如果没有右子树 则距离为0
LTree[x].dis = ;
else
LTree[x].dis = LTree[LTree[x].r].dis + ;
return x;
}
int del(int x)
{ //返回删除根以后左右子树的合并的根
int l,r;
l=LTree[x].l;
r=LTree[x].r;
father[l]=l;
father[r]=r;
LTree[x].l=LTree[x].r=LTree[x].dis=;
return Merge(l,r);
}
void solve(int x,int y)
{
LTree[x].strong /= ;
LTree[y].strong /= ;
//问每次PK以后,当前这个群体里力量最大的猴子的力量是多少。
int left,right;
left = del(x);
right = del(y);
left = Merge(left,x);
right = Merge(right,y);
left = Merge(left,right);
printf("%d\n",LTree[left].strong);
}
int main()
{
int n,m,x,y;
while (scanf("%d",&n)!=EOF)
{
for (int i= ;i<=n ;i++)
{
scanf("%d",<ree[i].strong);
LTree[i].l=;
LTree[i].r=;
LTree[i].dis=;
father[i]=i; //起始已自己为父亲
}
scanf("%d",&m);
for (int i= ;i<=m ;i++)
{
scanf("%d%d",&x,&y);
int fx=Find(x),fy=Find(y);
if (fx == fy) printf("-1\n");
else solve(fx,fy);
}
}
return ;
}
hdu 1512 Monkey King 左偏树的更多相关文章
- hdu 1512 Monkey King —— 左偏树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...
- HDU 1512 Monkey King (左偏树+并查集)
题意:在一个森林里住着N(N<=10000)只猴子.在一开始,他们是互不认识的.但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识 (认识具有传递性)的两只猴子之间.争斗时,两只猴子都 ...
- HDU 1512 Monkey King ——左偏树
[题目分析] 也是堆+并查集. 比起BZOJ 1455 来说,只是合并的方式麻烦了一点. WA了一天才看到是多组数据. 盲人OI (- ̄▽ ̄)- Best OI. 代码自带大常数,比启发式合并都慢 [ ...
- HDU 1512 Monkey King(左偏堆)
爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...
- ZOJ2334 Monkey King 左偏树
ZOJ2334 用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用. 左偏树代码的核心也是两棵树的合并! 代码有些细节需要注意. #include&l ...
- zoj 2334 Monkey King/左偏树+并查集
原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389 大致题意:N只相互不认识的猴子(每只猴子有一个战斗力值) 两只 ...
- HDU1512 ZOJ2334 Monkey King 左偏树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - ZOJ2334 题目传送门 - HDU1512 题意概括 在一个森林里住着N(N<=10000)只猴子. ...
- 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 : ...
随机推荐
- 求:斐波那契数列的第n项
def he (n): if n < 3 : return 1 return he(n-1)+he(n-2)print(he(n))
- 1064 Complete Binary Search Tree (30 分)(二叉查找树)
中序遍历建树 #include<bits/stdc++.h> using namespace std; ; int s[N]; int n; int tree[N]; int cnt; v ...
- shell之基本语法
转: read 命令从 stdin 获取输入并赋值给 PERSON 变量,最后在 stdout 上输出: #!/bin/bash # Script follows here: echo " ...
- django通用视图之TemplateView和ListView简单介绍
django支持类视图,与此同时django为我们提供了许多非常好用的通用视图供我们使用,这其中TemplateView.ListView和DetailView是我们经常使用到的,这里就对Templa ...
- luajit的字节码
http://blog.csdn.net/zzz3265/article/details/41146569 这里写出了luajit的字节码
- Redis键管理
Redis键管理 Redis 键命令用于管理 redis 的键. 语法 Redis 键命令的基本语法如下: redis > COMMAND KEY_NAME redis > SET w3c ...
- 自动驾驶缺人才?听听David Silver怎么说!
如今自动驾驶在全球范围内的发展势头愈发“凶猛”,该领域人才也一度被视为“香饽饽”. 即使在美国,自动驾驶工程师的起薪也已经突破了25万美元,我国‘“开价”之高更是令人咋舌. 人才.人才.还是人才!重要 ...
- POJ 2406 Power Strings 暴力
emmmm 显然的是a串长度是s串长度的因数 我们可以暴力枚举因数然后暴力check #include<cstdio> #include<algorithm> #include ...
- Codeforces Round #421 (Div. 2) D. Mister B and PR Shifts
Codeforces Round #421 (Div. 2) D. Mister B and PR Shifts 题意:给一个长度为\(n\)的排列,每次可以向右循环移位一次,计算\(\sum_{i= ...
- BZOJ2208 [Jsoi2010]连通数 【图的遍历】
题目 输入格式 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i行第j列的1表示顶点i到j有边,0则表示无边. 输出格式 输出一行一个整数,表示该图的连通数. 输入样例 3 ...