CF1076E.Vasya and a Tree

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Vasya has a tree consisting of nn vertices with root in vertex 11. At first all vertices has 00 written on it.

Let d(i,j)d(i, j) be the distance between vertices ii and jj, i.e. number of edges in the shortest path from ii to jj. Also, let's denote kk-subtree of vertex xx — set of vertices yy such that next two conditions are met:

  • xx is the ancestor of yy (each vertex is the ancestor of itself);
  • d(x,y)≤kd(x, y) \le k.

Vasya needs you to process mm queries. The ii-th query is a triple viv_i, did_i and xix_i. For each query Vasya adds value xix_i to each vertex from did_i-subtree of viv_i.

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

瓦西娅有一棵由 nn 个顶点构成的树,根节点为顶点 11。初始时,所有顶点上的数值均为 00。

设 d(i,j)d(i, j) 表示顶点 ii 与 jj 之间的距离,即从 ii 到 jj 的最短路径所含边的数量。此外,定义顶点 xx 的 kk-子树为满足以下两个条件的所有顶点 yy 构成的集合:

  • xx 是 yy 的祖先(每个顶点均视为自身的祖先);
  • d(x,y)≤kd(x, y) \le k。

瓦西娅需要你处理 mm 个查询。第 ii 个查询是一个三元组 viv_i、did_i 和 xix_i。对于每个查询,瓦西娅将值 xix_i 加到 viv_i 的 did_i-子树中所有顶点上。

请向瓦西娅报告:在处理完所有查询后,树中各个顶点上所写的数值。

输入格式

The first line contains single integer nn (1≤n≤3⋅1051 \le n \le 3 \cdot 10^5) — number of vertices in the tree.

Each of next n−1n - 1 lines contains two integers xx and yy (1≤x,y≤n1 \le x, y \le n) — edge between vertices xx and yy. It is guarantied that given graph is a tree.

Next line contains single integer mm (1≤m≤3⋅1051 \le m \le 3 \cdot 10^5) — number of queries.

Each of next mm lines contains three integers viv_i, did_i, xix_i (1≤vi≤n1 \le v_i \le n, 0≤di≤1090 \le d_i \le 10^9, 1≤xi≤1091 \le x_i \le 10^9) — description of the ii-th query.

第一行包含一个整数 nn(1≤n≤3⋅1051 \le n \le 3 \cdot 10^5)——树中顶点的数量。

接下来的 n−1n - 1 行每行包含两个整数 xx 和 yy(1≤x,y≤n1 \le x, y \le n)——表示顶点 xx 与 yy 之间的一条边。保证给定的图是一棵树。

下一行包含一个整数 mm(1≤m≤3⋅1051 \le m \le 3 \cdot 10^5)——查询的数量。

接下来的 mm 行每行包含三个整数 viv_i、did_i、xix_i(1≤vi≤n1 \le v_i \le n,0≤di≤1090 \le d_i \le 10^9,1≤xi≤1091 \le x_i \le 10^9)——第 ii 个查询的描述。

输出格式

Print nn integers. The ii-th integers is the value, written in the ii-th vertex after processing all queries.

输出 nn 个整数。其中第 ii 个整数表示处理完所有查询后,第 ii 个顶点上所写的值。

输入输出样例

  • 输入#1

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

    输出#1

    1 11 1 100 0
  • 输入#2

    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

    输出#2

    10 24 14 11 11

说明/提示

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

在第一个示例中,顶点的初始值为 0,0,0,0,00, 0, 0, 0, 0。第一次查询后,各顶点的值变为 1,1,1,0,01, 1, 1, 0, 0。第二次查询后,各顶点的值变为 1,11,1,0,01, 11, 1, 0, 0。第三次查询后,各顶点的值变为 1,11,1,100,01, 11, 1, 100, 0。

输入解题思路,AI测评打分。不知道怎么写?

首页