洛谷P3066 [USACO12DEC] 逃跑的Barn [左偏树]
逃跑的Barn
题目描述
It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to round them all up, and needs your help in the search.
FJ's farm is a series of N (1 <= N <= 200,000) pastures numbered 1...N connected by N - 1 bidirectional paths. The barn is located at pasture 1, and it is possible to reach any pasture from the barn.
FJ's cows were in their pastures this morning, but who knows where they ran to by now. FJ does know that the cows only run away from the barn, and they are too lazy to run a distance of more than L. For every pasture, FJ wants to know how many different pastures cows starting in that pasture could have ended up in.
Note: 64-bit integers (int64 in Pascal, long long in C/C++ and long in Java) are needed to store the distance values.
给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个。
输入输出格式
输入格式:
* Line 1: 2 integers, N and L (1 <= N <= 200,000, 1 <= L <= 10^18)
* Lines 2..N: The ith line contains two integers p_i and l_i. p_i (1 <= p_i < i) is the first pasture on the shortest path between pasture i and the barn, and l_i (1 <= l_i <= 10^12) is the length of that path.
输出格式:
* Lines 1..N: One number per line, the number on line i is the number pastures that can be reached from pasture i by taking roads that lead strictly farther away from the barn (pasture 1) whose total length does not exceed L.
输入输出样例
4 5
1 4
2 3
1 5
3
2
1
1
说明
Cows from pasture 1 can hide at pastures 1, 2, and 4.
Cows from pasture 2 can hide at pastures 2 and 3.
Pasture 3 and 4 are as far from the barn as possible, and the cows can hide there.
分析:
一句话:左偏树。
做多了这类题目的话,这题也就不难想。从根节点向下$DFS$,然后递归回溯,到达某个结点时就把它和它的子树构成的左偏树合并,这里左偏树维护的权值是该点到根节点的距离,判断的时候用类似于前缀和的思想就行了。
Code:
//It is made by HolseLee on 28th Aug 2018
//Luogu.org P3066
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std; typedef long long ll;
const int N=2e5+;
int n,ans[N],nxt[N],head[N],to[N],cnte,rt[N];
ll m,v[N];
struct Leftist{
int ch[N][],dis[N],siz[N];
ll val[N]; 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);
int &ul=ch[x][], &ur=ch[x][];
ur=merge(ur,y);
if( dis[ur]>dis[ul] ) swap(ur,ul);
dis[x]=dis[ur]+;
siz[x]=siz[ur]+siz[ul]+;
return x;
} inline int delet(int x)
{
return merge(ch[x][],ch[x][]);
}
}T; template<typename re>
inline void read(re &x)
{
x=; char ch=getchar(); bool flag=false;
while( ch<''||ch>'' ) {
if( ch=='-' )flag=true;
ch=getchar();
}
while( ch>=''&&ch<='' ) {
x=(x<<)+(x<<)+(ch^);
ch=getchar();
}
x=flag?-x:x;
} void dfs(int u,ll now)
{
rt[u]=u;
T.val[u]=now, T.siz[u]=, T.dis[u]=;
T.ch[u][]=T.ch[u][]=;
for(int i=head[u]; i; i=nxt[i]) {
dfs(to[i],now+v[i]);
rt[u]=T.merge(rt[u],rt[to[i]]);
}
while( rt[u] && (T.val[rt[u]]-now)>m ) rt[u]=T.delet(rt[u]);
ans[u]=T.siz[rt[u]];
} int main()
{
read(n); read(m);
int x; ll y;
for(int i=; i<=n; ++i) {
read(x); read(y);
to[++cnte]=i;v[cnte]=y;
nxt[cnte]=head[x];head[x]=cnte;
}
dfs(,);
for(int i=;i<=n;++i)printf("%d\n",ans[i]);
return ;
}
洛谷P3066 [USACO12DEC] 逃跑的Barn [左偏树]的更多相关文章
- P3066 [USACO12DEC] 逃跑的Barn 左偏树
P3066 逃跑的Barn 左偏树 题面 题意:给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 注意到答案的两个性质: 一个点的所有答案一定包含在其所有儿子的答案中 如 ...
- 洛谷P3066 [USACO12DEC]逃跑的Barn (线段树合并)
题目描述It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to ro ...
- 洛谷.3273.[SCOI2011]棘手的操作(左偏树)
题目链接 还是80分,不是很懂. /* 七个操作(用左偏树)(t2表示第二棵子树): 1.合并:直接合并(需要将一个t2中原有的根节点删掉) 2.单点加:把这个点从它的堆里删了,加了再插入回去(有负数 ...
- 洛谷P3273 [SCOI2011] 棘手的操作 [左偏树]
题目传送门 棘手的操作 题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 ...
- 洛谷P4331 [BOI2004] Sequence 数字序列 [左偏树]
题目传送门 数字序列 题目描述 给定一个整数序列 a1,a2,⋅⋅⋅,an ,求出一个递增序列 b1<b2<⋅⋅⋅<bn ,使得序列 ai 和 bi 的各项之差的绝对 ...
- 模板 可并堆【洛谷P3377】 【模板】左偏树(可并堆)
P3377 [模板]左偏树(可并堆) 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或第y个数已经被删 ...
- 洛谷$P4331\ [BOI2004]\ Sequence$ 数字序列 左偏树
正解:左偏树 解题报告: 传送门$QwQ$ 开始看到的时候$jio$得长得很像之前做的一个$dp$,,, 但是$dp$那题是说不严格这里是严格? 不难想到我们可以让$a_{i},b_{i}$同时减去$ ...
- 洛谷P3066 [USACO12DEC]逃跑的BarnRunning Away From…
题面链接 一句话题意:给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 我:似乎并不好做啊...看了题解后大雾... sol:考虑树上差分,对于一个点,在他那个位置++, ...
- 洛谷P3261 [JLOI2015]城池攻占(左偏树)
传送门 每一个城市代表的点开一个小根堆,把每一个骑士合并到它开始攻占的城池所代表的点上 然后开始dfs,每一次把子树里那些还活着的骑士合并上来 然后再考虑当前点的堆,一直pop直到骑士全死光或者剩下的 ...
随机推荐
- JS中client/offset/scroll等的宽高解析
原文地址:→传送门 window相关宽高属性 1. window.outerHeight (窗口的外层的高度) / window.outerWidth (窗口的外层的宽度) window.outerH ...
- Intellij IDEA 快捷键整理(转)
Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表达式时按 “!”键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更改的文件 Shift+Click,可以关闭文件 C ...
- 调试android chrome web page简明备忘
必备工具 adb tools.android chrome 先开启手机调试模式 adb forward tcp:9919 localabstract:chrome_devtools_remote 成功 ...
- 数据库-Core Data
技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong 数据库-Core Data 技术博客htt ...
- python学习笔记(十三)之lambda表达式
lambda表达式: 用法 lambda x : 2 * x + 1 其中:前面是参数,后面是返回值. >>> def ds(x): ... return 2 * x + 1 ... ...
- gcc 随笔
将几个文件编译成一个动态库 libtest.so gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so 将test.c与动态库libte ...
- 【洛谷 P3227】 [HNOI2013]切糕(最小割)
题目链接 每层每个位置向下一层这个位置连边,流量为下一层这个位置的\(f\),源点向第一层连,流量第一层每个位置的费用,最后一层向汇点连,流量\(INF\). 这样就得到了\(P*Q\)条链,不考虑\ ...
- 吐泡泡(2018年全国多校算法寒假训练营练习比赛(第二场)+栈模拟)+Plug-in(codeforces81A+栈模拟)
吐泡泡题目链接:https://www.nowcoder.com/acm/contest/74/A 题目: 思路: 这种题目当初卡了我很久,今天早训时遇到一个一样得题,一眼就想到用栈模拟,就又回来把这 ...
- 残差网络(Residual Network)
一.背景 1)梯度消失问题 我们发现很深的网络层,由于参数初始化一般更靠近0,这样在训练的过程中更新浅层网络的参数时,很容易随着网络的深入而导致梯度消失,浅层的参数无法更新. 可以看到,假设现在需要更 ...
- windows安装linux虚拟机、修改apt源
记录一下windows安装虚拟机以及初始配置的一些坑. 安装VMware Workstation 直接百度搜索VMware,选择合适的版本下载: 按照一般软件的安装步骤安装VMware Worksta ...