令牌重新移动AI (CI翻译)
You are given a tree with $n$ vertices, numbered from $1$ to $n$. Initially, vertex $i$ contains $a_i$ tokens.
给定一棵有 $n$ 个顶点的树,顶点编号为 $1$ 到 $n$。初始时,顶点 $i$ 上有 $a_i$ 个令牌。AI (CI翻译)
Suppose the tree is rooted at some vertex $r$. An operation on a vertex $u$ moves all tokens currently located in the subtree$^{\text{∗}}$ of $u$ to vertex $u$. The cost of this operation is the number of tokens at vertex $u$ after the move.
假设以某个顶点 $r$ 为根。对顶点 $u$ 执行一次操作,会将当前位于 $u$ 的子树$^{\text{∗}}$中的所有令牌移到顶点 $u$。这次操作的代价为操作后顶点 $u$ 上的令牌数。AI (CI翻译)
You are also given a permutation $p_1, p_2, \ldots, p_n$ of the integers from $1$ to $n$. The weight of $p$ is the total cost of performing the operations on vertices $p_1, p_2, \ldots, p_n$ in this order.
另给定一个由 $1$ 到 $n$ 组成的排列 $p_1, p_2, \ldots, p_n$。排列 $p$ 的权重为按顺序对顶点 $p_1, p_2, \ldots, p_n$ 执行操作的总代价。AI (CI翻译)
For each $r$ from $1$ to $n$, find the weight of $p$ when the tree is rooted at vertex $r$. For each $r$, the operations are performed independently, starting from the initial placement of tokens. Since the answers might be large, output them modulo $998\,244\,353$.
对于每个 $r$($1$ 到 $n$),求以顶点 $r$ 为根时排列 $p$ 的权重。对于每个 $r$,操作都独立进行,且从令牌的初始分布开始。由于答案可能很大,请将答案对 $998\,244\,353$ 取模后输出。AI (CI翻译)
$^{\text{∗}}$A subtree of vertex $u$ is the subgraph consisting of $u$, all its descendants, and all the edges between them.
$^{\text{∗}}$顶点 $u$ 的子树是由 $u$、它的所有后代以及它们之间的所有边组成的子图。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
每个输入文件包含多组测试用例。第一行包含测试用例数 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)
The first line of each test case contains a single integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of vertices in the tree.
每个测试用例的第一行包含一个整数 $n$($1 \le n \le 5 \cdot 10^5$)——树的顶点数。AI (CI翻译)
Each of the next $n - 1$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \ne v$) — an edge between vertices $u$ and $v$. It is guaranteed that the given edges form a tree.
接下来 $n - 1$ 行,每行包含两个整数 $u$ 和 $v$($1 \le u, v \le n$,$u \ne v$),表示顶点 $u$ 和顶点 $v$ 之间有一条边。保证给出的边构成一棵树。AI (CI翻译)
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \lt 998\,244\,353$), where $a_i$ is the initial number of tokens at vertex $i$.
接下来一行包含 $n$ 个整数 $a_1, a_2, \ldots, a_n$($0 \le a_i \lt 998\,244\,353$),其中 $a_i$ 表示顶点 $i$ 上的初始令牌数。AI (CI翻译)
The next line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, all $p_i$ are distinct) — the permutation $p$.
接下来一行包含 $n$ 个整数 $p_1, p_2, \ldots, p_n$($1 \le p_i \le n$,且两两不同),表示排列 $p$。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
保证所有测试用例的 $n$ 之和不超过 $5 \cdot 10^5$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, output $n$ integers, where the $r$-th integer is the weight of $p$ when the tree is rooted at vertex $r$, modulo $998\,244\,353$.
对于每个测试用例,输出 $n$ 个整数,其中第 $r$ 个整数表示以顶点 $r$ 为根时排列 $p$ 的权重,对 $998\,244\,353$ 取模。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, consider $r = 1$. Initially, the token counts are $[1]$. The operation on vertex $p_1 = 1$ moves the tokens of the subtree $\{1\}$ to vertex $1$, so its cost is $1$. Thus, the weight of $p$ is $1$.
在第一个测试用例中,考虑 $r = 1$ 的情况。初始令牌数为 $[1]$。对顶点 $p_1 = 1$ 执行操作,会将子树 ${1}$ 中的令牌移到顶点 $1$,因此代价为 $1$。所以排列 $p$ 的权重为 $1$。AI (CI翻译)
In the second test case, consider $r = 1$. Initially, the token counts are $[5, 13]$.
在第二个测试用例中,考虑 $r = 1$ 的情况。初始令牌数为 $[5, 13]$。AI (CI翻译)
- The operation on vertex
$p_1 = 2$moves the tokens of the subtree$\{2\}$to vertex$2$, so its cost is$13$. The token counts stay$[5, 13]$. - The operation on vertex
$p_2 = 1$moves the tokens of the subtree$\{1, 2\}$to vertex$1$, so its cost is$18$. The token counts become$[18, 0]$.
- 对顶点
$p_1 = 2$执行操作,会将子树${2}$中的令牌移到顶点$2$,因此代价为$13$。令牌数仍为$[5, 13]$。 - 对顶点
$p_2 = 1$执行操作,会将子树${1, 2}$中的令牌移到顶点$1$,因此代价为$18$。令牌数变为$[18, 0]$。
Thus, the weight of $p$ is $13 + 18 = 31$.
所以排列 $p$ 的权重为 $13 + 18 = 31$。AI (CI翻译)
In the third test case, consider $r = 1$. Initially, the token counts are $[7, 2, 9]$.
在第三个测试用例中,考虑 $r = 1$ 的情况。初始令牌数为 $[7, 2, 9]$。AI (CI翻译)
- The operation on vertex
$p_1 = 2$moves the tokens of the subtree$\{2\}$to vertex$2$, so its cost is$2$. The token counts stay$[7, 2, 9]$. - The operation on vertex
$p_2 = 3$moves the tokens of the subtree$\{3\}$to vertex$3$, so its cost is$9$. The token counts stay$[7, 2, 9]$. - The operation on vertex
$p_3 = 1$moves the tokens of the subtree$\{1, 2, 3\}$to vertex$1$, so its cost is$18$. The token counts become$[18, 0, 0]$.
- 对顶点
$p_1 = 2$执行操作,会将子树${2}$中的令牌移到顶点$2$,因此代价为$2$。令牌数仍为$[7, 2, 9]$。 - 对顶点
$p_2 = 3$执行操作,会将子树${3}$中的令牌移到顶点$3$,因此代价为$9$。令牌数仍为$[7, 2, 9]$。 - 对顶点
$p_3 = 1$执行操作,会将子树${1, 2, 3}$中的令牌移到顶点$1$,因此代价为$18$。令牌数变为$[18, 0, 0]$。
Thus, the weight of $p$ is $2 + 9 + 18 = 29$.
所以排列 $p$ 的权重为 $2 + 9 + 18 = 29$。AI (CI翻译)
样例
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
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。