题目描述:

Alyona and a tree

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).

Let's define dist(v, u) as the sum of the integers written on the edges of the simple path from v to u.

The vertex v controls the vertex u (v ≠ u) if and only if u is in the subtree of v and dist(v, u) ≤ au.

Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such that vcontrols u.

Input

The first line contains single integer n (1 ≤ n ≤ 2·105).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers written in the vertices.

The next (n - 1) lines contain two integers each. The i-th of these lines contains integers pi and wi (1 ≤ pi ≤ n, 1 ≤ wi ≤ 109) — the parent of the (i + 1)-th vertex in the tree and the number written on the edge between pi and (i + 1).

It is guaranteed that the given graph is a tree.

Output

Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.

Examples

input

5

2 5 1 4 6

1 7

1 1

3 5

3 6

output

1 0 1 0 0

input

5

9 7 8 6 5

1 1

2 1

3 1

4 1

output

4 3 2 1 0

Note

In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1controls the vertex 5).

思路:

这道题是说,给一棵树,树上的每个节点都有一个a[i]值,每条边上都有权重。如果对一个节点来说,他到子树下面某个节点的距离(权值和)小于等于那个节点的a[i]值,那它就能控制这个节点。那么现在对于每个节点,看它能控制多少个节点。

如果我们先dfs一下这棵树,就可以求出来所有节点的深度(带权值)。然后怎么办?难道要看结点1,下面的结点有几个满足被控制条件,结点2,看下面有几个满足被控制的条件...?想想时间复杂度就很高,而且该用什么数据结构实现呢(我是蒟蒻我不知道)。

那转换一下关注对象,上面我们看的是对每个节点看他能控制那些节点,不如这样:对于每个节点我们看他能被那些节点控制。具体的,对节点u,如果p(p是u的祖先节点)满足\(depth[u]-depth[p]\leq a[u]\),那u就能被p控制。又发现这样一个事实,树上从上往下深度是递增的,意味着如果p满足了控制u的条件,那么p之下,u之上的节点都能控制u。因为到u的距离小于p到u的距离,从而小于a[u]。单增序列,我们只要找到遍历路径上第一个满足控制u的条件的节点就行了。这时,我们可以建一个\(vector\)来存储路径,路径是边,第一个属性是这条边到达点深度,第二个属性是到达点的标号。由于序列的单调性,使用lower_bound即可求出p节点来。

现在就是要把p节点下u节点上的节点的控制个数加一了,对于区间加法,我们可以考虑差分数组,在这里,也就是树上差分。具体的,将p节点父亲的控制数减一(如果找得到的话),将u节点的父结点控制数加u节点的控制数再加一。每个节点在\(dfs\)回溯时都做类似处理,到达p点就会得到正确的答案。

注意的是及时更新路径,包括加入新的边和弹出已返回的边(才知道原来\(vector\)有个pop_back()函数,真方便)。

代码:

#include <iostream>
#include <vector>
#define max_n 200005
using namespace std;
typedef pair<int,long long> PIL;
typedef pair<long long,int> PLI;
vector<PIL> edge[max_n];
int ans[max_n];
int n;
int a[max_n];
vector<PLI> path;
long long depth[max_n];
void dfs(int s)
{
/*cout << "s " << s << endl;
cout << "path" << path.size() << endl;
for(int i = 0;i<path.size();i++)
{
cout << path[i].first << " " << path[i].second << endl;
}*/
int node = lower_bound(path.begin(),path.end(),PLI(depth[s]-a[s],-1))-path.begin()-1;
//cout << "node " << node << endl;
if(node>=0) ans[path[node].second]--;
path.push_back(PLI(depth[s],s));
for(int i = 0;i<edge[s].size();i++)
{
int v = edge[s][i].first;
long long w = edge[s][i].second;
depth[v] = depth[s]+w;
dfs(v);
ans[s] += ans[v]+1;
}
path.pop_back();
}
int main()
{
cin >> n;
for(int i = 1;i<=n;i++)
{
cin >> a[i];
}
int v;
long long w;
for(int i = 1;i<n;i++)
{
cin >> v >> w;
edge[v].push_back(PIL(i+1,w));
}
dfs(1);
for(int i = 1;i<=n;i++)
{
cout << ans[i] << " ";
}
cout << endl;
return 0;
}

参考文章:

qscqesze,Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和,https://www.cnblogs.com/qscqesze/p/6103445.html(是大佬,不做过多解释)

键盘里的青春,Codeforces 739B Alyona and a tree (树上差分+二分),https://blog.csdn.net/qq_34374664/article/details/70246427(为什么又是他?别问,问就是带佬)

Codeforces E. Alyona and a tree(二分树上差分)的更多相关文章

  1. CodeForces 739B Alyona and a tree (二分+树上差分)

    <题目链接> 题目大意: 给定一颗带权树,树的根是1,树上每个点都有点权,并且还有边权.现在给出“控制”的定义:对一个点u,设v为其子树上的节点,且$dis(u,v)≤val[v]$,则称 ...

  2. 【CF739B】Alyona and a tree(树上差分,二分,树形DP)

    题意:给出一棵有根树,树上每个点.每条边都有一个权值. 现在给出“控制”的定义:对一个点u,设点v在其子树上,且dis(u,v)≤av,则称u控制v. 要求求出每个点控制了多少个点 n (1 ≤ n  ...

  3. Codeforces 682C Alyona and the Tree (树上DFS+DP)

    题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...

  4. CodeForces 682C Alyona and the Tree (树上DFS)

    题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...

  5. 洛谷 P2680 运输计划-二分+树上差分(边权覆盖)

    P2680 运输计划 题目背景 公元 20442044 年,人类进入了宇宙纪元. 题目描述 公元20442044 年,人类进入了宇宙纪元. L 国有 nn 个星球,还有 n-1n−1 条双向航道,每条 ...

  6. P2680 运输计划(二分+树上差分)

    P2680 运输计划 链接 分析: 二分+树上差分. 首先可以二分一个答案,那么所有比这个答案大的路径,都需要减去些东西才可以满足这个答案. 那么减去的这条边一定在所有的路径的交集上. 那么如果求快速 ...

  7. P2680 运输计划 二分+树上差分

    又咕咕了几天\(QwQ\) 思路:二分+树上差分 提交:\(\geq5\)次 错因:\(lca\)写错+卡了很久常数(哪位大佬帮我康康,有更好的写法请指出\(QwQ\)) 题解: 我们先将原问题转化为 ...

  8. XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)

    D. Alyona and a tree time limit per test  2 seconds memory limit per test  256 megabytes input  stan ...

  9. Codeforces 739B Alyona and a tree(树上路径倍增及差分)

    题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...

随机推荐

  1. php 回调函数结合闭包(匿名函数)的使用示例

    <?php /** * php 回调函数结合闭包(匿名函数)的使用 */ function callback( $callback ){ $variable = 'program'; $ret1 ...

  2. AngularJS 常见面试问题

    ng-if 跟 ng-show/hide 的区别有哪些? 第一点区别是,ng-if 在后面表达式为 true 的时候才创建这个 dom 节点,ng-show 是初始时就创建了,用 display:bl ...

  3. 修改IP地址之后认证信息问题

    $ ssh lvph@172.16.20.20 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOT ...

  4. git对某个文件取消跟踪

    git rm --cached readme1.txt    删除readme1.txt的跟踪,并保留在本地. git rm --f readme1.txt    删除readme1.txt的跟踪,并 ...

  5. matlab 双坐标折线图画法

    %%各时段电量需求 clc close all clear all cost_gd = [2200 1800 3800 4600]; cost_bj = [2.7 2.2 1.8 3.6]; cost ...

  6. SpringBoot2.x+Redis+nginx实现session共享和负载均衡

    1.创建SpringBoot项目添加依赖 <dependency> <groupId>org.springframework.session</groupId> & ...

  7. 通过对比ASCII编码来理解Unicode编码

    Unicode是个规范,可以理解为一个索引表,世界上所有字符基本上在这个索引表中都能找到唯一一个数码与之对应,就像ASCII码表一样,也是一个规范,也可以看成是一个索引表,所有的英文字符都可以在这个索 ...

  8. ASP.NET SignalR 系列(四)之指定对象推送

    在上一章讲到了广播推送,即所有订阅的用户都能收到,这种适合于信息广播. 接下来介绍如何给指定的对象推送 在讲这个之前先说明一下连接创建的基础知识 1.每个页面与服务端创建连接并启动时,这时服务端会产生 ...

  9. springboot笔记10——整合Redis

    依赖 <dependencies> <!--web依赖--> <dependency> <groupId>org.springframework.boo ...

  10. win 修改notebook路径

    开始发现 notebook 默认的路径是 C:\Users\Administrator 需要修改 将目标中的%USERPROFILE% 直接删掉了