#C4903. Subtree Sum Calculator

    ID: 48493 Type: Default 1000ms 256MiB

Subtree Sum Calculator

Subtree Sum Calculator

You are given a tree with (n) nodes where each node has a specific intrinsic value. The tree is an undirected connected acyclic graph. For each node, its subtree value is defined as the sum of its own value plus the values of all nodes in the subtree rooted at that node, where the tree is considered rooted at node 1.

You will be given (t) test cases. For each test case, the input is provided in the following format:

  1. An integer (n), the number of nodes.
  2. A line with (n) integers, where the (i^{th}) integer represents the intrinsic value of node (i).
  3. (n - 1) lines, each containing two integers (u) and (v), representing an undirected edge between nodes (u) and (v).

For each test case, output a single line containing (n) integers separated by a space, where the (i^{th}) integer is the sum of values in the subtree rooted at node (i).

inputFormat

The input begins with an integer (t) on a single line, representing the number of test cases. For each test case, the following lines are provided:

  • A line containing a single integer (n), the number of nodes in the tree.
  • A line containing (n) space-separated integers, the intrinsic values of the nodes (from node 1 to node (n)).
  • (n - 1) lines, each containing two space-separated integers (u) and (v), denoting an edge between nodes (u) and (v).

All input is provided via standard input (stdin).

outputFormat

For each test case, output a single line to standard output (stdout) containing (n) space-separated integers. The (i^{th}) integer should be the sum of all node values in the subtree rooted at node (i) (with the tree rooted at node 1).## sample

1
1
10
10

</p>