题目大意:

给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于m的点有多少个

左偏树 https://blog.csdn.net/pengwill97/article/details/82874235

题解https://www.cnblogs.com/GXZlegend/p/6532881.html

若y在x的子树内 那么x到y的距离 等于 dis(1,y)-dis(1,x)

所以DFS时处理出节点到根(点1)的距离

然后自底向上合并 维护距离大顶堆

那么当 堆顶到根的距离 > m+当前点到根的距离 时 说明距离大于m

同时也说明再继续向上回溯合并时 这个堆顶对应的点与那些点的距离也肯定会超过m

所以直接将堆顶踢出堆 即合并其左儿子与右儿子

#include <bits/stdc++.h>
#define INF 0x3f3f3f
#define LL long long
#define mem(i,j) memset(i,j,sizeof(i))
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
const int N=2e5+;
const int mod=1e9+; int n;
LL L, v[N];
int dis[N], ans[N];
int l[N], r[N], root[N];
struct NODE { int to; LL len; };
vector <NODE> G[N];
void init() {
mem(dis,); mem(v,);
inc(i,,n) G[i].clear();
}
void addE(int u,int v,LL w) {
G[u].push_back({v,w});
}
int unite(int x,int y) {
if(!x) return y;
if(!y) return x;
if(v[x]<v[y]) swap(x,y);
r[x]=unite(r[x],y);
if(dis[l[x]]<dis[r[x]]) swap(l[x],r[x]);
dis[x]=dis[r[x]]+;
return x;
}
void DFS(int u) {
root[u]=u; ans[u]=;
int len=G[u].size()-;
inc(i,,len) {
NODE e=G[u][i];
v[e.to]=v[u]+e.len;
DFS(e.to);
ans[u]+=ans[e.to];
root[u]=unite(root[u],root[e.to]);
}
while(v[root[u]]>L+v[u]) {
ans[u]--;
root[u]=unite(l[root[u]],r[root[u]]);
}
} int main()
{
while(~scanf("%d%lld",&n,&L)) {
init();
inc(i,,n) {
int v; LL w;
scanf("%d%lld",&v,&w);
addE(v,i,w);
}
dis[]=-; DFS();
inc(i,,n) printf("%d\n",ans[i]);
} return ;
}

USACO Running Away From the Barn /// 可并堆 左偏树维护大顶堆的更多相关文章

  1. 洛谷P3066 [USACO12DEC] 逃跑的Barn [左偏树]

    题目传送门 逃跑的Barn 题目描述 It's milking time at Farmer John's farm, but the cows have all run away! Farmer J ...

  2. P3066 [USACO12DEC] 逃跑的Barn 左偏树

    P3066 逃跑的Barn 左偏树 题面 题意:给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 注意到答案的两个性质: 一个点的所有答案一定包含在其所有儿子的答案中 如 ...

  3. bzoj3011 [Usaco2012 Dec]Running Away From the Barn 左偏树

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3011 题解 复习一下左偏树板子. 看完题目就知道是左偏树了. 结果这个板子还调了好久. 大概已 ...

  4. [USACO 12DEC]Running Away From the Barn

    Description It's milking time at Farmer John's farm, but the cows have all run away! Farmer John nee ...

  5. BZOJ 3011: [Usaco2012 Dec]Running Away From the Barn( dfs序 + 主席树 )

    子树操作, dfs序即可.然后计算<=L就直接在可持久化线段树上查询 -------------------------------------------------------------- ...

  6. BZOJ_3011_[Usaco2012 Dec]Running Away From the Barn _可并堆

    BZOJ_3011_[Usaco2012 Dec]Running Away From the Barn _可并堆 Description 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于l的 ...

  7. 【BZOJ3011】[Usaco2012 Dec]Running Away From the Barn 可并堆

    [BZOJ3011][Usaco2012 Dec]Running Away From the Barn Description It's milking time at Farmer John's f ...

  8. USACO Section 5.3 Big Barn(dp)

    USACO前面好像有类似的题目..dp(i,j)=min(dp(i+1,j),dp(i+1,j+1),dp(i,j+1))+1  (坐标(i,j)处无tree;有tree自然dp(i,j)=0) .d ...

  9. USACO 6.1 A Rectangular Barn

    A Rectangular Barn Mircea Pasoi -- 2003 Ever the capitalist, Farmer John wants to extend his milking ...

随机推荐

  1. 推荐Windows下SVN服务器端和客户端工具软件

    相信很多人使用过Windows下的SVN客户端软件TortoiseSVN或者也有过Linux下.MAC下的SVN命令行使用经验,另外MAC下还有以一款就做Vesions的SVN客户端软件,不过个人感觉 ...

  2. grafana初体验

    1.centos版下载安装 wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.0.4-1.x86_6 ...

  3. 使用Condition实现顺序执行

    参考<Java多线程编程核心技术> 使用Condition对象可以对线程执行的业务进行排序规划 具体实现代码 public class Run2 { private static Reen ...

  4. byte为什么要与0xff

    面对带正负号的数,会采用符号扩展,如果原值是正数,则高位补上0:如果原值是负数,高位补1.二进制是计算技术中广泛采用的一种数制.二进制数据是用0和1两个数码来表示的数.当前的计算机系统使用的基本上是二 ...

  5. flex布局(弹性布局)

    1. 传统布局与 flex 布局比较 传统布局 兼容性好 布局繁琐 局限性,不能在移动端很好的布局 flex 弹性布局 操作方便,布局极为简单,移动端应用很广泛 PC端浏览器支持较差 IE 11 或 ...

  6. Vs 中的智能提示,默认选中,切换快捷键

    切换是否默认选中,快捷键: Ctrl+Alt+Space(空格)

  7. shell常见的返回状态码

  8. Linux——用户及文件权限管理

    2019-07-31 用户管理 查看用户 who am i:打开当前伪终端的用户的用户名 pts/0 后面那个数字就表示打开的伪终端序号,你可以尝试再打开一个终端,然后在里面输入 who am i , ...

  9. php 学习一 变量的定义

    //php有如下几种数据类型 // false true boolean类型 //integer int 整数 //float 浮点数就是小数 //string 字符串 //string null 空 ...

  10. php自动生成不重复的id

    PHP uniqid()函数可用于生成不重复的唯一标识符,该函数基于微秒级当前时间戳.在高并发或者间隔时长极短(如循环代码)的情况下,会出现大量重复数据.即使使用了第二个参数,也会重复,最好的方案是结 ...