USACO Running Away From the Barn /// 可并堆 左偏树维护大顶堆
题目大意:
给出以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 /// 可并堆 左偏树维护大顶堆的更多相关文章
- 洛谷P3066 [USACO12DEC] 逃跑的Barn [左偏树]
题目传送门 逃跑的Barn 题目描述 It's milking time at Farmer John's farm, but the cows have all run away! Farmer J ...
- P3066 [USACO12DEC] 逃跑的Barn 左偏树
P3066 逃跑的Barn 左偏树 题面 题意:给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 注意到答案的两个性质: 一个点的所有答案一定包含在其所有儿子的答案中 如 ...
- bzoj3011 [Usaco2012 Dec]Running Away From the Barn 左偏树
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3011 题解 复习一下左偏树板子. 看完题目就知道是左偏树了. 结果这个板子还调了好久. 大概已 ...
- [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 ...
- BZOJ 3011: [Usaco2012 Dec]Running Away From the Barn( dfs序 + 主席树 )
子树操作, dfs序即可.然后计算<=L就直接在可持久化线段树上查询 -------------------------------------------------------------- ...
- BZOJ_3011_[Usaco2012 Dec]Running Away From the Barn _可并堆
BZOJ_3011_[Usaco2012 Dec]Running Away From the Barn _可并堆 Description 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于l的 ...
- 【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 ...
- 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 ...
- USACO 6.1 A Rectangular Barn
A Rectangular Barn Mircea Pasoi -- 2003 Ever the capitalist, Farmer John wants to extend his milking ...
随机推荐
- java.lang
package cn.sxt; import java.lang.*; public class BooleanDemo { public static void main(Stri ...
- echarts 视图自适应问题
最近在项目中用到了echarts,在处理视图自适应问题上记录一下:同时比较一下和highcharts的区别: 在echarts中有一个resize的函数,可以直接在监听窗口变化时重新渲染即可: //在 ...
- firefox error downloading
转自:http://blog.csdn.net/feigeswjtu/article/details/42146285 做过互联网开发的都知道,firefox是我们互联网开发必备浏览器之一,浏览器是载 ...
- document.readyState和document.DOMContentLoaded判断DOM的加载完成
document.readyState:判断文档是否加载完成.firefox不支持. 这个属性是只读的,传回值有以下的可能: 0-UNINITIALIZED:XML 对象被产生,但没有任何文件被加载. ...
- win10 虚拟机VMware 14中CentOS7文件共享
一,环境 主机:win10 家庭版 软件:VMware 14 系统:CentOS 7 二,设置共享文件 右键虚拟机->选择设置 如图:创建共享文件 三,安装VMware Tools 然后进入ce ...
- 笔记62 Spring Boot快速入门(二)
SpringBoot部署 一.jar方式 1.首先安装maven. <1>下载最新的maven版本:https://maven.apache.org/download.cgi <2& ...
- androidstudio 2.3.3 jni过程汇总(2):2、使用so文件
2.使用so文件 1.在java文件中System.loadLibrary加载包,并且引入native方法. 2.在app/src/main/下新建jniLibs文件夹,将so包带arm文件夹形式导入 ...
- mongodb的学习 (1)
1. 启动服务 (1)在D盘下新建mongodb 文件夹,在mongodb 文件夹里面新建db; (2)win+R 启动cmd ,输入 `mongod --dbpath "D:/mongod ...
- Array Beauty
Array Beauty 给出一个长度为n的序列\(\{a_i\}\),定义一个序列的权值为其中元素两两之差的绝对值的最小值,询问\(\{a_i\}\)长度为K的子序列的权值之和\(\% 998244 ...
- Circular Coloring
Circular Coloring 将n个0,m个1进行圆周排列,定义一个排列的权值为圆上所有相邻且相同的数字组成一段的段长的乘积,询问断环成链所有方案的权值之和,\(n,m\leq 5000\). ...