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 ...
随机推荐
- PHP中使用raw格式发送POST请求
如果请求的参数格式是原生(raw)的内容,应该如何为程序构造一个POST请求函数呢? function http_post($url, $data_string) { $ch = curl_init( ...
- 用Emacs进行Java开发
用Emacs进行Java开发 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} 用 ...
- multiple-cursors实在是太好用了
multiple-cursors实在是太好用了 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #8 ...
- python- 属性 静态方法,类方法
一,面向对象结构与成员 1,1 面向对象结构分析: 那么每个大区域又可以分为多个小部分: class A: company_name = '老男孩教育' # 静态变量(静态字段) __iphone = ...
- koa2实现登录注册功能(ejs+mongodb版)
gtihub仓库地址:(由于国内处于敏感时期,github暂时无法访问) 主要使用的中间件: "ejs": "^2.7.1",(渲染模板) "koa& ...
- KiCAD层颜色修改
KiCAD层颜色修改 KiCAD的PCB各层的颜色太过于暗淡,有时可能不适合操作者的习惯,尤其是铜层(布线层),这时候就需要去修改层的颜色,具体操作如下图:选择想要修改的层,双击左边颜色框框,进入之后 ...
- codeforces round 433 C. Planning 贪心
题目大意: 输入n,k,代表n列航班,初始始发实践为1,2,3分钟以此类推,然后输入n个整数分别代表延迟1分钟第i个航班损失多少钱,然后调整后的始发时间表是这样的,任何一辆航班的始发时间不能在他的初始 ...
- Java中使用try-catch-finally处理IO流中的异常
我们使用try-catch-finally来接收IO流的异常 finally是最后执行的步骤,非常适合最后存放close来关闭IO流,而且编程中我们不可以随意抛出异常,必须对异常进行处理. 从try- ...
- nodejs的桌面应用(electron)
最近发现nodejs可以做桌面应用,主要是之前的同事在搞,我也要稍微研究下不能落后啊,基于nodejs的桌面应用,常用的就是nw.js和electron,nw出的比较早,资料比较多,bug也很多,它的 ...
- Delphi中点击网页弹出的Alert对话框的确定按钮
思路: 使用Windows API函数遍历窗口,查找指定标题的窗口,然后从该窗口查找确定按钮,向该按钮发送鼠标消息进行模拟点击.由于IE8由Alert弹出的网页对话框的标题是“来自网页的消息”,而IE ...