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 ...
随机推荐
- span 设置inline-block 写文字的span错位
写一个如下图这样排版 设置几个span为inline-block 中间的span写了文字的span错位了 解决方案 给span添加 vertical-align: top
- Spring Cloud配置中心客户端读取配置
微服务连接配置中心来实现外部配置的读取. 引入依赖 <dependencies> <dependency> <groupId>org.springframework ...
- Smokeping分享
Somkeping master端搭建过程 服务端/master端 一. 搭好环境,安装依赖包(1)yum -y install httpd fping echoping curl rrdtool ...
- 基于Linux平台病毒Wirenet.c解析
在分析Wirenet.c时,感觉自己学到了非常多非常赞的思想,希望跟大家一同交流. 转载请注明出处:http://blog.csdn.net/u010484477谢谢^_^ watermark/2/t ...
- C++之循环体内变量
今天做PAT题目时候看人家解答: #include <cstdio> #include <set> using namespace std; int main() { int ...
- 深入Linux内核架构 - 内核之中数据结构之间的关系图 & 设备驱动程序(转)
内核之中数据结构之间的关系图 设备驱动程序
- android studio 设计任务内容和识别内容界面 (android stuido design task layout)
本人android studio版本是 3.4.1,设计了一个任务内容和识别内容的界面,欢迎大家品尝. 界面显示如下图所示: 实现代码如下: <?xml version="1.0&qu ...
- mysql查找字段空、不为空的方法总结
1.不为空 Select * From table_name Where id<>'' Select * From table_name Where id!='' 2.为空 Select ...
- php封装支付
<?php namespace Pay; use think\Config; vendor ('Pay.alipay.pagepay.service.AlipayTradeService'); ...
- SQl 分组后按照某一列拼接字符串
/* 分组之后拼接字符串 */ ;with t as( SELECT WorkflowId,Remark FROM dbo.OperatorAutomationProcess GROUP BY Wor ...