题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=3011

题解

复习一下左偏树板子。

看完题目就知道是左偏树了。

结果这个板子还调了好久。

大概已经不会写左偏树了。


时间复杂度 \(O(n\log n)\)。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 200000 + 7; #define lc c[0]
#define rc c[1] int n; ll l;
int ans[N], rt[N];
ll dis[N]; struct Edge { int to, ne; ll w; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y, ll z) { g[++tot].to = y, g[tot].w = z, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y, ll z) { addedge(x, y, z), addedge(y, x, z); } struct Node { int c[2], dis, s; ll v; } t[N]; inline int merge(int o, int p) {
if (!o || !p) return o ^ p;
if (t[o].v < t[p].v) std::swap(o, p);
t[o].rc = merge(t[o].rc, p);
if (t[t[o].lc].dis < t[t[o].rc].dis) std::swap(t[o].lc, t[o].rc);
t[o].dis = t[t[o].rc].dis + 1, t[o].s = t[t[o].lc].s + t[t[o].rc].s + 1;
return o;
}
inline void pop(int &o) { o = merge(t[o].lc, t[o].rc); } inline void dfs(int x, int fa = 0) {
rt[x] = x, t[x].v = dis[x], t[x].s = 1;
for fec(i, x, y) if (y != fa) dis[y] = dis[x] + g[i].w, dfs(y, x), rt[x] = merge(rt[x], rt[y]);
while (t[rt[x]].v > dis[x] + l) pop(rt[x]);
ans[x] = t[rt[x]].s;
} inline void work() {
for (int i = 1; i <= n; ++i) if (!dis[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
} inline void init() {
read(n), read(l);
int x; ll y;
for (int i = 2; i <= n; ++i) read(x), read(y), adde(x, i, y);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj3011 [Usaco2012 Dec]Running Away From the Barn 左偏树的更多相关文章

  1. [BZOJ3011][Usaco2012 Dec]Running Away From the Barn

    题意 给出一棵以1为根节点树,求每个节点的子树中到该节点距离<=l的节点的个数 题解 方法1:倍增+差分数组 首先可以很容易的转化问题,考虑每个节点对哪些节点有贡献 即每次对于一个节点,找到其第 ...

  2. 【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 ...

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

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

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

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

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

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

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

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

  7. [Usaco2012 Dec]Running Away From the Barn

    题目描述 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 输入格式 Line 1: 2 integers, N and L (1 <= N <= 200,0 ...

  8. USACO Running Away From the Barn /// 可并堆 左偏树维护大顶堆

    题目大意: 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于m的点有多少个 左偏树 https://blog.csdn.net/pengwill97/article/details/82 ...

  9. [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 ...

随机推荐

  1. 前端Node项目发布流程

    最近在做前端的发布流程,发布流程的主要实现以下几个方面: 构建:包括JavaScript.css.html等的压缩,以及版本控制,利用md5生成版本号替换文件引用,实现长缓存策略. 发布:输出新版本的 ...

  2. hdu6599 I Love Palindrome String

    由样例可知,题目中求的回文串数量,其实是本质不同的回文串数量,这个可以直接用回文树来做. 考虑前半段是回文串这个限制,这个东西回文树不好做,可以再套一个马拉车,然后记录一下插入到回文树的节点中最后一个 ...

  3. Python Module_subprocess_调用 Powershell

    目录 目录 前言 Powershell call Python Python call Powershell Powershell发送邮件 最后 前言 使用Python内建的subprocess模块, ...

  4. 阶段1 语言基础+高级_1-3-Java语言高级_09-基础加强_第3节 注解_18_注解_案例_简单的测试框架

    定义计算器的类 用注解的方式去测试计算器类里面 所有的方法 想验证哪个方法 就在方法的上面加上注解@check 执行TestCheck验证方法 控制台的输出 根目录生成了一个 bug.txt文件 重写 ...

  5. webService框架CXF的简单使用

    最近本来已经将上一个项目交活,全身心投入了另外项目的前端的开发工作.可之前的项目经理通知我,之前的项目需要做一个webService的功能,于是稍微做了一下webService,可是忘了通知我现在的项 ...

  6. 整理一下go的ci工具

    代码格式化 go fmt fileName.go goimports 自动格式化import goimports -w fileName.go mod 自动更新/删除包 go mod tidy 检查注 ...

  7. wcf restful 访问报错 *.svc HTTP error 404.17 - Not Found

    安装完成 iisreset,即使不重启也已经可以使用了

  8. JSON.toJSONString时保留null值

    QuoteFieldNames———-输出key时是否使用双引号,默认为true WriteMapNullValue——–是否输出值为null的字段,默认为false WriteNullNumberA ...

  9. mangde 的服务启动

    启动命令如下

  10. Insertion Sort List(单链表插入排序)

    来源:https://leetcode.com/problems/insertion-sort-list Sort a linked list using insertion sort. 方法: 1. ...