Ensure that you are logged in and have the required permissions to access the test.

Rhezo and Tree

0

0 votes
Very-Easy
Problem

You are given a rooted tree (rooted at 1) with N nodes. Each node has some value associated with it. Let Pi denote the number of paths from node i to a leaf node.

For each of the Pi paths, calculate the product of node values on the path. For each node i, you need to find the sum of these products.

Input:

First line of input contains an integer N. Each of the next N1 lines contains 2 integers A and B, meaning that there is an edge between A and B. Next line contains N space separated integers denoting the node values.

Output:

Print a line with N space separated integers, each of which is the required sum modulo 109+7.

Constraints:

1N106

1A,BN

0 node values 109

Sample Input
5
1 2
2 3
2 4
2 5
1 8 4 2 3
Sample Output
72 72 164 114 147
Time Limit: 3
Memory Limit: 256
Source Limit:
Editor Image

?