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. mysql 5.7主从安装和配置

    本文主要介绍mysql 5.7主从复制,转载请注明出处 下载地址 模块 版本 下载地址 mysql 5.7 https://dev.mysql.com/downloads/mysql/ libaio( ...

  2. 【抽象那些事】不完整的抽象&多方面抽象&未用的抽象&重复的抽象

    不完整的抽象 抽象未支持所有互补或相关的方法时,将导致这种坏味. 为什么要有完整的抽象? 一种重要的抽象实现手法是创建内聚而完整的抽象.抽象未支持相关的方法时,可能会影响抽象的内聚性和完整性.如果抽象 ...

  3. Install OpenCV 3.0 and Python 2.7+ on OSX

    http://www.pyimagesearch.com/2015/06/15/install-OpenCV-3-0-and-Python-2-7-on-osx/ As I mentioned las ...

  4. Kotlin : Retrofit + RxAndroid + Realm

    https://jqs7.com/kotlin-retrofit-rxandroid-realm/ 原作者:Ahmed Rizwan 原文链接:Kotlin : Retrofit + RxAndroi ...

  5. Java自学?Java编程资源大放送

    黑马程序员 北京JavaEE就业班32期教程视频+源码+资料 链接: https://pan.baidu.com/s/1VCXyNVD-LvlZyReVgzKXGg 密码:cike 黑马:Java基础 ...

  6. Spark学习笔记

    Map-Reduce 我认为上图代表着MapReduce不仅仅包括Map和Reduce两个步骤这么简单,还有两个隐含步骤没有明确,全部步骤包括:切片.转换.聚合.叠加,按照实际的运算场景上述步骤可以简 ...

  7. html5 兼容版本 video

    <!-- first try HTML5 playback: if serving as XML, expand `controls` to `controls="controls&q ...

  8. Install and Configure Apache Kafka on Ubuntu 16.04

    https://devops.profitbricks.com/tutorials/install-and-configure-apache-kafka-on-ubuntu-1604-1/ by hi ...

  9. 关于loadrunner使用web_add_header添加HTTP信息头(比如Content-Type,token等)和使用

    关于loadrunner使用web_add_header添加HTTP信息头(比如Content-Type,token等)和使用 1.web_add_header添加HTTP信息头(比如Content- ...

  10. SQL Server中表锁定的原理及解锁演示

    有几个朋友留言建议结合例子来演示一下, 上篇已经说过锁的几种类型, 可以利用系统动态视图sys.dm_tran_locks查看到,重要的栏位如下: resource_type 被锁的资源类型(Data ...