Token Re-Moving
Taylor Swift - Patient Zero
You are given a tree with vertices, numbered from to . Initially, vertex contains tokens.
Suppose the tree is rooted at some vertex . An operation on a vertex moves all tokens currently located in the subtree of to vertex . The cost of this operation is the number of tokens at vertex after the move.
You are also given a permutation of the integers from to . The weight of is the total cost of performing the operations on vertices in this order.
For each from to , find the weight of when the tree is rooted at vertex . For each , the operations are performed independently, starting from the initial placement of tokens. Since the answers might be large, output them modulo .
A subtree of vertex is the subgraph consisting of , all its descendants, and all the edges between them.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer () — the number of vertices in the tree.
Each of the next lines contains two integers and (, ) — an edge between vertices and . It is guaranteed that the given edges form a tree.
The next line contains integers (), where is the initial number of tokens at vertex .
The next line contains integers (, all are distinct) — the permutation .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output integers, where the -th integer is the weight of when the tree is rooted at vertex , modulo .
Note
In the first test case, consider . Initially, the token counts are . The operation on vertex moves the tokens of the subtree to vertex , so its cost is . Thus, the weight of is .
In the second test case, consider . Initially, the token counts are .
- The operation on vertex moves the tokens of the subtree to vertex , so its cost is . The token counts stay .
- The operation on vertex moves the tokens of the subtree to vertex , so its cost is . The token counts become .
Thus, the weight of is .
In the third test case, consider . Initially, the token counts are .
- The operation on vertex moves the tokens of the subtree to vertex , so its cost is . The token counts stay .
- The operation on vertex moves the tokens of the subtree to vertex , so its cost is . The token counts stay .
- The operation on vertex moves the tokens of the subtree to vertex , so its cost is . The token counts become .
Thus, the weight of is .
6 1 1 1 2 1 2 5 13 2 1 3 1 2 1 3 7 2 9 2 3 1 4 1 2 1 3 2 4 3 1 4 1 3 2 4 1 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 17 23 31 12 8 45 19 62 4 3 6 1 7 8 2 5 10 1 2 1 3 2 4 2 5 3 6 3 7 4 8 5 9 6 10 101 203 157 59 311 83 97 127 41 67 3 10 2 8 1 7 5 6 9 4
1 31 18 29 18 20 15 13 9 21 540 557 363 217 426 300 436 591 2391 1650 1246 2837 2544 2409 2395 2710 3749 2342
Discussions & Comments
Share your approach or ask questions about this problem (0)
You must be logged in to comment or start a discussion on this problem.
Log InNo comments yet. Be the first to comment!
Actions
Open on CodeforcesAbout Platform
Problem Discussions
0Have questions or want to share your solution approach? Join the discussion.
View / write comments