Description

It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to round them all up, and needs your help in the search.

FJ's farm is a series of N (1 <= N <= 200,000) pastures numbered 1...N connected by N - 1 bidirectional paths. The barn is located at pasture 1, and it is possible to reach any pasture from the barn.

FJ's cows were in their pastures this morning, but who knows where they ran to by now. FJ does know that the cows only run away from the barn, and they are too lazy to run a distance of more than L. For every pasture, FJ wants to know how many different pastures cows starting in that pasture could have ended up in.

Note: 64-bit integers (int64 in Pascal, long long in C/C++ and long in Java) are needed to store the distance values.

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

Input

  • Line 1: 2 integers, N and L (1 <= N <= 200,000, 1 <= L <= 10^18)

  • Lines 2..N: The ith line contains two integers p_i and l_i. p_i (1 <= p_i < i) is the first pasture on the shortest path between pasture i and the barn, and l_i (1 <= l_i <= 10^12) is the length of that path.

Output

  • Lines 1..N: One number per line, the number on line i is the number pastures that can be reached from pasture i by taking roads that lead strictly farther away from the barn (pasture 1) whose total length does not exceed L.

Sample Input

4 5
1 4
2 3
1 5

Sample Output

3
2
1
1

Hint

Cows from pasture 1 can hide at pastures 1, 2, and 4.

Cows from pasture 2 can hide at pastures 2 and 3.

Pasture 3 and 4 are as far from the barn as possible, and the cows can hide there.

题解

简要来说,左偏树

具体思想是:先$Dfs$求出根节点到各个节点的距离,再按逆$Dfs$时间戳顺序进行操作(为了使得处理的当前节点的所有子节点均被处理过,至于为何不正向,就不解释了)

建大根堆,每次做完合并操作后,将不可行的边从堆中弹出(即堆顶所表示的点到当前点的距离$>L$(同时以操作顺序为前提的条件下必有“相距距离=两点到根节点的距离差”))

另一个需要解决的问题就是如何求解,我们可以按逆$Dfs$序模拟一个回溯过程:将所以$pop$掉的值和其子节点的值累加,再相减即可。

 #include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const long long N=;
struct tt
{
long long cost,next,to;
}edge[*N+];//保存边的信息
long long path[N+],top;
struct node
{
long long key,dist;
node *l,*r;
long long ldist() {return l ? l->dist:-;}
long long rdist() {return r ? r->dist:-;}
}T[N+],*root[N+];//T[i]表示节点i的相关信息;root[i]表示序号为i的节点所在堆的根的地址
long long n,l,a,b;
long long remain[N+],tail,Rank[N+];//remain[]表示逆Dfs顺序,tail表示remain[]的大小;Rank[]表示Bfs序
long long popnum[N+],cnt[N+];//popnum[i]保存在i节点时,弹出元素的数量 cnt[i]表示以i为根,其子树节点数量(不含根节点)
void Add(long long x,long long y,long long cost);
void Dfs(long long x);
node* Merge(node* a,node* b);
int main()
{
scanf("%lld%lld",&n,&l);
for (long long i=;i<=n;i++)
{
scanf("%lld%lld",&a,&b);
Add(a,i,b);
Add(i,a,b); }//连双向边,正向用于Dfs用,逆向用于求解用 Rank[]=;
Dfs();
for (long long i=;i<=tail;i++)
{
for (long long j=path[remain[i]];j;j=edge[j].next)
{
if (Rank[remain[i]]==Rank[edge[j].to]+)//找到前驱节点
{
root[edge[j].to]=Merge(root[remain[i]],root[edge[j].to]);//将当前节点构成的堆并入前驱节点
while(root[edge[j].to]->key-T[edge[j].to].key>l)//弹出
{
popnum[edge[j].to]++;
root[edge[j].to]=Merge(root[edge[j].to]->l,root[edge[j].to]->r);
}
}
}
}
for (long long i=;i<=tail;i++) //对最终答案数据的处理
{
for (long long j=path[remain[i]];j;j=edge[j].next)
{
if (Rank[remain[i]]==Rank[edge[j].to]+)
{
cnt[edge[j].to]+=cnt[remain[i]]+;
popnum[edge[j].to]+=popnum[remain[i]];
}
}
}
for (long long i=;i<=n;i++) printf("%lld\n",cnt[i]+-popnum[i]);
return ;
}
void Add(long long x,long long y,long long cost)
{
edge[++top].to=y;
edge[top].cost=cost;
edge[top].next=path[x];
path[x]=top;
}
void Dfs(long long x)
{
root[x]=x+T;
for (long long i=path[x];i;i=edge[i].next) if (!Rank[edge[i].to])
{
Rank[edge[i].to]=Rank[x]+;
T[edge[i].to].key=T[x].key+edge[i].cost;//key保存的是根节点到该点的距离
Dfs(edge[i].to);
}
remain[++tail]=x;
}
node* Merge(node* a,node* b)
{
if (!a||!b) return a ? a:b;
if (a->key<b->key) swap(a,b);
a->r=Merge(a->r,b);
if (a->ldist()<a->rdist()) swap(a->l,a->r);
a->dist=a->rdist()+;
return a;
}

[USACO 12DEC]Running Away From the Barn的更多相关文章

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

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

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

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

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

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

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

  5. [Usaco2012 Dec]Running Away From the Barn

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

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

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

  7. USACO 2008 Running(贝茜的晨练)

    [题解] 动态规划,dp[i][j]表示第i分钟疲劳度为j的最长距离. [代码] #include <iostream> #include <cstdlib> #include ...

  8. 洛谷P1353 USACO 跑步 Running

    题目 一道入门的dp,首先要先看懂题目要求. 容易得出状态\(dp[i][j]\)定义为i时间疲劳度为j所得到的最大距离 有两个坑点,首先疲劳到0仍然可以继续疲劳. 有第一个方程: \(dp[i][0 ...

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

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

随机推荐

  1. java中的异常以及 try catch finally以及finally的执行顺序

    java中的 try.catch.finally及finally执行顺序详解: 1.首相简单介绍一下异常以及异常的运行情况: 在Java中异常的继承主要有两个: Error和Exception 这两个 ...

  2. JAVA中GridBagLayout布局管理器应用详解

    很多情况下,我们已经不需要通过编写代码来实现一个应用程序的图形界面,而是通过强大的IDE工具通过拖拽辅以简单的事件处理代码即可很轻松的完成.但是我们不得不面对这样操作存在的一些问题,有时候我们希望能够 ...

  3. XCode Build Settings中几种Search Paths

    Header search path:去查找头文件的路径,同在在你需要使用第三方库的时候,在这里设置你的头文件路径目录,如图 <code><span class="str& ...

  4. OptaPlanner - 把example运行起来(运行并浅析Cloud balancing)

    经过上面篇长篇大论的理论之后,在开始讲解Optaplanner相关基本概念及用法之前,我们先把他们提供的示例运行起来,好先让大家看看它是如何工作的.OptaPlanner的优点不仅仅是提供详细丰富的文 ...

  5. python使用tesseract-ocr完成验证码识别(安装部分)

    一.tesseract-ocr安装 Ubuntu版本: 1.tesseract-ocr安装 sudo apt-get install tesseract-ocr 2.pytesseract安装 sud ...

  6. php的调试工具xdebug

    zend_extension = "D:/developsoftware/wamp/bin/php/php5.5.12/zend_ext/php_xdebug-2.2.5-5.5-vc11- ...

  7. C# 使用 GDI+ 给图片添加文字,并使文字自适应矩形区域

    需求 需求是要做一个编辑文字的页面.用户在网页端写文字,文字区域是个矩形框,用户可以通过下方的拖动条调节文字大小. 如下图: 提交数据的时候前端传文字区域的左上角和右下角定位给后台.因为前端的字体大小 ...

  8. DSkin 的WebUI开发模式介绍,Html快速开发Winform的UI

    新版WebUI开发模式采用MiniBlink内核,这个内核功能更完善,dll压缩之后才5M,而且提供开发者功能,内核还在更新中,而且是开源项目:https://github.com/weolar/mi ...

  9. OAuth2.0学习(1-2)OAuth2.0的一个企业级应用场景 - 新浪开放平台微博OAuth2.0认证

    http://open.weibo.com/wiki/%E9%A6%96%E9%A1%B5 开发者可以先浏览OAuth2.0的接口文档,熟悉OAuth2.0的接口及参数的含义,然后我们根据应用场景各自 ...

  10. Django(博客系统):基于pycharm如何一个django工程下创建多个app

    背景:通常我们创建一个django系统时,为了把业务模块划分清楚往往会把一个独立的业务模块放到一个app中,如果多个独立的业务模块就会创建多个app,一般情况下为了更好的管理这些app,会把他们都存放 ...