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

Description

给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于l的点有多少个。

Sample Input

4 5
1 4
2 3
1 5

Sample Output

3
2
1
1


做法不唯一,这里用来练习可并堆。

先求出每个点$i$ 到根路径上的长度$dis[i]$ ,对每个点建一个可并堆(大根)。

然后从下往上合并,如果当前$dis[堆顶]-dis[x]>L$ 就弹出,记录每个节点最后剩下的点数即可。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 200050
typedef long long ll;
ll val[N<<1],L,v[N];
int head[N],to[N<<1],nxt[N<<1],cnt,n,root[N],ls[N],rs[N],dis[N],siz[N];
inline void add(int u,int v,ll w) {
to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; val[cnt]=w;
}
int merge(int x,int y) {
if(!x) return y;
if(!y) return x;
if(v[x]<v[y]) swap(x,y);
rs[x]=merge(rs[x],y);
if(dis[ls[x]]<dis[rs[x]]) swap(ls[x],rs[x]);
dis[x]=dis[rs[x]]+1;
return x;
}
void dfs(int x,int y) {
int i;
siz[x]=1; root[x]=x;
for(i=head[x];i;i=nxt[i]) {
if(to[i]!=y) {
v[to[i]]=v[x]+val[i];
dfs(to[i],x);
siz[x]+=siz[to[i]];
root[x]=merge(root[x],root[to[i]]);
}
}
while(v[root[x]]-v[x]>L) {
siz[x]--; root[x]=merge(ls[root[x]],rs[root[x]]);
}
}
int main() {
dis[0]=-1;
scanf("%d%lld",&n,&L);
int i,x;
ll y;
for(i=2;i<=n;i++) {
scanf("%d%lld",&x,&y);
add(i,x,y); add(x,i,y);
}
dfs(1,0);
for(i=1;i<=n;i++) {
printf("%d\n",siz[i]);
}
}

BZOJ_3011_[Usaco2012 Dec]Running Away From the Barn _可并堆的更多相关文章

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

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

  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. [BZOJ3011][Usaco2012 Dec]Running Away From the Barn

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

  4. [Usaco2012 Dec]Running Away From the Barn

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

  5. bzoj3011 [Usaco2012 Dec]Running Away From the Barn 左偏树

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3011 题解 复习一下左偏树板子. 看完题目就知道是左偏树了. 结果这个板子还调了好久. 大概已 ...

  6. BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序

    BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序 题意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最 ...

  7. 【BZOJ3012】[Usaco2012 Dec]First! Trie树+拓补排序

    [BZOJ3012][Usaco2012 Dec]First! Description Bessie has been playing with strings again. She found th ...

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

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

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

随机推荐

  1. org.eclipse.swt.SWTException: Invalid thread access问题解决方法

    转自 http://blog.csdn.net/ecjtuxuan/article/details/2125023 怎么解决SWT多线程错误:Caused by: org.eclipse.swt.SW ...

  2. Spring3 MVC

    一.前言: 大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了.Spring3 MVC结构简单,应了那句话简单就是美,而 ...

  3. Emit方式调用方法

    object objRet = Delegate.CreateDelegate(typeof(Func<Guid, int, decimal>), inst, "HelloWor ...

  4. Day8 接口与归一化设计

    接口:在程序的使用中,我不能把程序的主体直接提供给使用者,一般是提供一个接口. 为什么要使用接口: 1,接口提取了一群共同的函数,可以把接口当做一个函数的集合. 2,让子类去实现接口中的函数. 归一化 ...

  5. orderBy新写法

    通常,我们处理排序规则的处理方法是在sql 语句中order by create_time desc, 但是这时我们需要从控制器中一步步找到该方法,操作多. 我们试着将业务逻辑拆分到控制器 中, 把排 ...

  6. FileReader读取本地文件

    FileReader是一种异步读取文件机制,结合input:file可以很方便的读取本地文件. 一.input:type[file] file类型的input会渲染为一个按钮和一段文字.点击按钮可打开 ...

  7. 一个简单而实用的JQ插件——lazyload.js图片延迟加载插件

      前  言 Cherish 看多了炫酷的插件之后再来看这么一个小清新的东西,是不是突然感觉JQ插件感觉很友好了,简单强大最重要的是实用. 这篇文章将详细讲解一下lazyload.js的用法 lazy ...

  8. 五年级--python函数高级运用

    一.装饰器 二.迭代器 三.生成器 四.练习 一.装饰器 1.1 闭包函数用法 # 需求: # 执行一个函数前需要认证是否登录,如果登录则不需再登录. # 只认证一次,后续操作无需认证 # 要求认证使 ...

  9. hadoop is running beyond virtual memory limits问题解决

    单机搭建了2.6.5的伪分布式集群,写了一个tf-idf计算程序,分词用的是结巴分词,使用standalone模式运行没有任何问题,切换到伪分布式模式运行一直报错: hadoop is running ...

  10. 分布式消息队列XXL-MQ

    <分布式消息队列XXL-MQ>     一.简介 1.1 概述 XXL-MQ是一款轻量级分布式消息队列,支持串行.并行和广播等多种消息模型.现已开放源代码,开箱即用. 支持三种消息模式: ...