#CF739B. Alyona and a tree

Alyona and a tree

题目描述

Alyona has a tree with n n vertices. The root of the tree is the vertex 1 1 . In each vertex Alyona wrote an positive integer, in the vertex i i she wrote ai a_{i} . 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) dist(v,u) as the sum of the integers written on the edges of the simple path from v v to u u .

The vertex v v controls the vertex u u ( vu v≠u ) if and only if u u is in the subtree of v v and dist(v,u)<=au dist(v,u)<=a_{u} .

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

输入格式

The first line contains single integer n n ( 1<=n<=2105 1<=n<=2·10^{5} ).

The second line contains n n integers a1,a2,...,an a_{1},a_{2},...,a_{n} ( 1<=ai<=109 1<=a_{i}<=10^{9} ) — the integers written in the vertices.

The next (n1) (n-1) lines contain two integers each. The i i -th of these lines contains integers pi p_{i} and wi w_{i} ( 1<=pi<=n 1<=p_{i}<=n , 1<=wi<=109 1<=w_{i}<=10^{9} ) — the parent of the (i+1) (i+1) -th vertex in the tree and the number written on the edge between pi p_{i} and (i+1) (i+1) .

It is guaranteed that the given graph is a tree.

输出格式

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

题目大意

题目描述:

Alyona有一棵有 nn 个节点的树。这棵树的根节点是 11。在每个节点里,Alyona写了一个正整数,在节点 ii 她写了正整数 aia_i 。另外,她在这棵树上的每条边上写了一个正整数(不同边上可能有不同的数)。

让我们定义 dist(v,u)dist(v,u) 作为从 vvuu 的简单路径上的边权和。

当且仅当 uuvv 的子树中并且 dist(v,u)audist(v,u)\leq a_u,顶点 vv 控制顶点 u(vu)u(v\neq u)

Alyona想在某些顶点定居。为了做到这件事,她想知道在每个节点 vv 能控制几个节点。

输入格式:

第一行包含一个整数 n(1n2×105)n (1\leq n\leq 2\times 10^5)

第二行有 nn 个整数 a1,a2,,an(1ai109)a_1,a_2,\ldots,a_n(1\leq a_i\leq 10^9) ,作为节点 ii 的数。

下面的 n1n-1 行,每行有两个整数。第 ii 行包含整数 pi,wi(1pin,1wi109)p_i,w_i(1\leq p_i\leq n,1\leq w_i\leq 10^9) ,分别为节点 i+1i+1 的在树上的父节点和 pip_i(i+1)(i+1) 的边上的数字。

数据保证是个树。

输出格式:

输出 nn 个整数,第 ii 个数为节点 ii 能控制的点数。

样例说明:

在样例中,节点 11 控制了节点 33 ,节点 33 控制节点 55 (注意,这并不代表节点 11 控制了节点 55

Translated by @lolte

5
2 5 1 4 6
1 7
1 1
3 5
3 6

1 0 1 0 0

5
9 7 8 6 5
1 1
2 1
3 1
4 1

4 3 2 1 0

提示

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