题目传送门

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. debian配置国内软件源

    本例在debian:buster-slim docker镜像中实验通过 1.启动docker实例 docker run -it --name debian debian:buster-slim bas ...

  2. Python实现人脸识别

    识别图片 #coding=utf-8 import requests,cv2 import re import os import bs4 #2.读取图片 filename = 'E:/Python/ ...

  3. PADS LAYOUT的一般流程

    1.概述    本文档的目的在于说明使用PADS的印制板设计软件PowerPCB进行印制板设计的流程和一些注意事项,为一个工作组的设计人员提供设计规 范,方便设计人员之间进行交流和相互检查. 2.设计 ...

  4. org.hibernate.AssertionFailure: null id don't flus

    我的是字段编码和数据库不匹配,是爬的微博数据

  5. spotlight监控linux性能

    linux性能监控有很多工具,spotlight只是其中一种 目录 1.安装spotlight 2.参数认识 1.安装spotlight spotlight不仅仅只是监控linux,还可以完成数据库以 ...

  6. python3 基本数据类型_1

    不得已,要学习python3了,之前了解到py2与py3有很大不同,不过学起来才能感觉到,比如print. 不过,同样的代码,可以使用py3,py2执行,结果也相似,大家可以看看. 大概因为初学,还未 ...

  7. Neither abstinence nor excess ever renders man happy

    inch.n. 英寸 courageous.adj.勇敢的 porcelain.n.瓷器 adj.脆的 inventor. n. 发明者 trivial.adj. 不重要的 grove.n.小树林,果 ...

  8. 多变量分析绘图(hue参数)以及盒图和小提琴图

    1,函数stipplot() stipplot()函数用来画散点图,其x轴是离散型的变量 直接上代码 import seaborn as sns import numpy as np import p ...

  9. JAVA基础面向对象分析

    面向对象内存的分析: 一:内存的分类 1:栈(tack) 2:堆(heop) 3: 静态区 4:代码区 二:引用数据类型内存特点 三:引用数据类型传值的特点 四:引用数据类型在作为参数时的特点 面向对 ...

  10. 关于java范型

    1 范型只在编译阶段有效 编译器在编译阶段检查范型结果之后,就会将范型信息删除.范型信息不会进入运行时阶段. 泛型类型在逻辑上看以看成是多个不同的类型,实际上都是相同的基本类型. 2 不能对确定的范型 ...