CF-2277F•Codeforces

Token Re-Moving

data structuresdfs and similardsutrees

Taylor Swift - Patient Zero

You are given a tree with nn vertices, numbered from 11 to nn. Initially, vertex ii contains aia_i tokens.

Suppose the tree is rooted at some vertex rr. An operation on a vertex uu moves all tokens currently located in the subtree∗^{\text{∗}} of uu to vertex uu. The cost of this operation is the number of tokens at vertex uu after the move.

You are also given a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n of the integers from 11 to nn. The weight of pp is the total cost of performing the operations on vertices p1,p2,…,pnp_1, p_2, \ldots, p_n in this order.

For each rr from 11 to nn, find the weight of pp when the tree is rooted at vertex rr. For each rr, the operations are performed independently, starting from the initial placement of tokens. Since the answers might be large, output them modulo 998 244 353998\,244\,353.

∗^{\text{∗}}A subtree of vertex uu is the subgraph consisting of uu, 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 tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

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

Each of the next n−1n - 1 lines contains two integers uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v) — an edge between vertices uu and vv. It is guaranteed that the given edges form a tree.

The next line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai<998 244 3530 \le a_i \lt 998\,244\,353), where aia_i is the initial number of tokens at vertex ii.

The next line contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≤pi≤n1 \le p_i \le n, all pip_i are distinct) — the permutation pp.

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5.

Output

For each test case, output nn integers, where the rr-th integer is the weight of pp when the tree is rooted at vertex rr, modulo 998 244 353998\,244\,353.

Note

In the first test case, consider r=1r = 1. Initially, the token counts are [1][1]. The operation on vertex p1=1p_1 = 1 moves the tokens of the subtree {1}\{1\} to vertex 11, so its cost is 11. Thus, the weight of pp is 11.

In the second test case, consider r=1r = 1. Initially, the token counts are [5,13][5, 13].

  • The operation on vertex p1=2p_1 = 2 moves the tokens of the subtree {2}\{2\} to vertex 22, so its cost is 1313. The token counts stay [5,13][5, 13].
  • The operation on vertex p2=1p_2 = 1 moves the tokens of the subtree {1,2}\{1, 2\} to vertex 11, so its cost is 1818. The token counts become [18,0][18, 0].

Thus, the weight of pp is 13+18=3113 + 18 = 31.

In the third test case, consider r=1r = 1. Initially, the token counts are [7,2,9][7, 2, 9].

  • The operation on vertex p1=2p_1 = 2 moves the tokens of the subtree {2}\{2\} to vertex 22, so its cost is 22. The token counts stay [7,2,9][7, 2, 9].
  • The operation on vertex p2=3p_2 = 3 moves the tokens of the subtree {3}\{3\} to vertex 33, so its cost is 99. The token counts stay [7,2,9][7, 2, 9].
  • The operation on vertex p3=1p_3 = 1 moves the tokens of the subtree {1,2,3}\{1, 2, 3\} to vertex 11, so its cost is 1818. The token counts become [18,0,0][18, 0, 0].

Thus, the weight of pp is 2+9+18=292 + 9 + 18 = 29.

Sample 1
Input
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
Output
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 In

No comments yet. Be the first to comment!

Actions

Open on Codeforces

About Platform

CO
Codeforces
External Judge

Problem Discussions

0

Have questions or want to share your solution approach? Join the discussion.

View / write comments