题目链接:传送门

题目:

E. Vasya and a Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has a tree consisting of n
vertices with root in vertex 1. At first all vertices has 0

written on it.

Let d(i,j)
be the distance between vertices i and j, i.e. number of edges in the shortest path from i to j. Also, let's denote k-subtree of vertex x — set of vertices y

such that next two conditions are met:

    x

is the ancestor of y
(each vertex is the ancestor of itself);
d(x,y)≤k

    . 

Vasya needs you to process m
queries. The i-th query is a triple vi, di and xi. For each query Vasya adds value xi to each vertex from di-subtree of vi

.

Report to Vasya all values, written on vertices of the tree after processing all queries.
Input

The first line contains single integer n
(1≤n≤3105

) — number of vertices in the tree.

Each of next n−1
lines contains two integers x and y (1≤x,y≤n) — edge between vertices x and y

. It is guarantied that given graph is a tree.

Next line contains single integer m
(1≤m≤3105

) — number of queries.

Each of next m
lines contains three integers vi, di, xi (1≤vi≤n, 0≤di≤109, 1≤xi≤109) — description of the i

-th query.
Output

Print n
integers. The i-th integers is the value, written in the i

-th vertex after processing all queries.
Examples
Input
Copy

5
1 2
1 3
2 4
2 5
3
1 1 1
2 0 10
4 10 100

Output
Copy

1 11 1 100 0 

Input
Copy

5
2 3
2 1
5 4
3 4
5
2 0 4
3 10 1
1 2 3
2 3 10
1 1 7

Output
Copy

10 24 14 11 11 

Note

In the first exapmle initial values in vertices are 0,0,0,0,0
. After the first query values will be equal to 1,1,1,0,0. After the second query values will be equal to 1,11,1,0,0. After the third query values will be equal to 1,11,1,100,0.
View Code

相关文章:

  • 2022-02-06
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2021-08-11
  • 2021-12-12
  • 2022-12-23
  • 2021-07-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2021-07-14
相关资源
相似解决方案