题目传送门

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. Go Int转string几种方式性能测试

    Go Int转string几种方式性能测试 - 贤冰的博客 - CSDN博客 https://blog.csdn.net/flyfreelyit/article/details/79701577

  2. Ubuntu - apt 下载源设置为阿里的源

    # 备份 sources.list cp /etc/apt/sources.list /etc/apt/sources.list.bak # 切换为阿里的源 echo "deb http:/ ...

  3. Git - grafted 和 shallow update not allowed

    一般人对开源的模板进行修改是总会进行这样的一条龙操作 # 克隆最近一次提交 git clone xxx --depth 1 # 修改修改修改 提交提交提交 vim xxx git commit -am ...

  4. Java ——扩展:内部类 匿名内部类 IO file 设计模式

    内部类的拓展 定义类or方法内部的类 最外层的类只能使用public和默认修饰 class Demo { class A { } public static void main(String[] ar ...

  5. chrome 74 版本的chromedriver下载地址

    微信扫二维码关注我的公众号,回复chromedriver 即可获取windows,liunx,mac版本最新selenium-chromedriver

  6. 初学node.js-nodejs中实现用户注册路由

    经过前面几次的学习,已经可以做下小功能,今天要实现的事用户注册路由. 一.users_model.js  功能:定义用户对象模型 var mongoose=require('mongoose'), S ...

  7. k8s/01开启云原生之门(Mooc)

    一.kubernetes(k8s)基础知识 1.简介 在2017年Kubernetes战胜了两个强大的竞争对手Swarm和Mesos,成为容器管理与调度编排领域的首选平台和事实标准. 2014年k8s ...

  8. Java中的静态变量、静态方法、静态代码块

    转载自http://www.cnblogs.com/panjun-Donet/archive/2010/08/10/1796209.html (一)静态方法(1)在Java里,可以定义一个不需要创建对 ...

  9. 20191127 Spring Boot官方文档学习(4.13)

    4.13.Messaging Spring框架为与消息传递系统集成提供了广泛的支持,从使用JmsTemplate简化JMS API到完整的异步接收消息的基础结构.Spring AMQP为高级消息队列协 ...

  10. Docker开启ssh服务

    一.准备 apt-get update       更新环境 apt-get install vim      安装vim vim  /etc/apt/source.list     更换软件源, 我 ...