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 ...
随机推荐
- Opengl 之 窗口初体验 ------ By YDD的铁皮锅
大二的时候开始想着做游戏,因为学校的课程实在是无聊就想着做些有意义的事情.毕竟学了编程这一行就得做些实事,于是就在网上搜了一下图形编程,偶然的了解到了Opengl (同时还有Windows上的Dire ...
- Java finally块
try块也可以有零个或一个finally块. finally块总是与try块一起使用. 语法 finally块的语法是: 1 2 3 finally { // Code for finall ...
- JS轻松实现单击文本框弹出选择日期
我的开发工具是vs2005,你们可选择自己合适的开发工具 首先创建个文本框<input id="txFDate" name="txFDate" type= ...
- oracle数据库 唯一约束的创建与删除
1.创建索引: alter table TVEHICLE add constraint CHECK_ONLY unique (CNUMBERPLATE, CVIN, CPLATETYPE, DWQCH ...
- DB2连接
ibm_db.connect 创建非持久连接. ibm_db.pconnect 创建持久连接. 在最初的Python脚本请求之后,持久的连接保持打开状态,这允许后续的Python请求重新使用连接. 后 ...
- 关于软件IntelliJ IDEA的使用技巧(三)
二,IntelliJ IDEA的工具栏介绍 2,IntelliJ IDEA菜单栏 (9)Tools工具 ✌1.Tasks & Contexts: ✌2.Generate JavaDoc: ✌3 ...
- python基础【第六篇】
list列表 基本结构 lst =[1,2,3,5,6] 为什么学列表? 列表能够存储比字符串更多的数据 列表能够存储各种数据类型 列表的注意点 列表是有序的 列表是可变的,支持索引,切片,步 切片后 ...
- ivew Upload 上传图片组件
1. 先展示一个效果图 2.代码详解 <!-- 封面缩略图 --> <div class="pop-up-div pic"> <div class=& ...
- Jmeter服务器性能压测-用户登录实例CSV方式
为什么用CSV方式压测,因为用jdbc链接数据库,我发现数据库数据量量大的情况下,Jmeter会内存溢出 第一步:数据准备,根据登录接口需要的参数准备测试数据 例子中,测试的登录接口需要4个参数化数据 ...
- ethtool---查看网卡
ethtool 命令详解 命令描述: ethtool 是用于查询及设置网卡参数的命令. 使用概要:ethtool ethx //查询ethx网口基本设置,其中 x 是对应网卡的编号,如et ...