Hello 2026
F
Jumping Man
Codeforces
2500
原题
← EG →
翻译中…

You have a tree rooted at node $1$ with $n$ nodes. Each node has a lowercase English letter written on it.

翻译中…

For each integer $i$ from $1$ to $n$, please solve the following problem independently:

翻译中…
  • Consider the set of strings formed by the following process:You performed the above process exactly once for every possible path. Two paths are considered different if one node is visited in one path but not another.Now, you have obtained many strings. You want to know the sum of the square of the number of occurrences for each type of string. Since this answer might be very large, output its value modulo $998\,244\,353$.
  • Choose any node $u$ that is in the subtree of $i$ as your starting location.
    • Repeat 0 or more times:
    • Suppose you are currently on node $x$. Select a node $v$ that is in the subtree$^{\text{∗}}$ of node $x$, but you may not choose $v=x$. Move to node $v$. This process can be terminated at any point.
    • The characters obtained from all nodes you passed through (in order) are concatenated to form a string.
翻译中…

$^{\text{∗}}$A node $v$ is in another node $x$'s subtree if and only if the shortest path from node $1$ to node $v$ passes through node $x$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.

翻译中…

For each test case, the first line contains an integer $n$ ($1 \le n \le 5000$).

翻译中…

The next line is a string of length $n$ containing only lowercase English letters, where the $i$-th character represents the letter on the $i$-th node.

翻译中…

This is followed by $n-1$ lines, each containing two integers $u$ and $v$ ($1 \leq u,v \leq n, u \neq v$) representing an edge of the tree.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$.

翻译中…

Output

翻译中…

For each test case, output $n$ numbers on a new line: the answer for $i=1,2,\ldots,n$, modulo $998\,244\,353$.

翻译中…

Note

翻译中…

For the first test case:

翻译中…
  • For nodes $2$ and $3$, the only string that is possible to get from the process is b. Therefore, the answer is $1$ for both.
  • For node $1$, the possible strings are a, ab, ab, b, and b. Overall, a is obtainable in one way, while ab and b are obtainable in two ways. Therefore, the answer for node $1$ is $1^2+2^2+2^2=9$.
翻译中…

In the fourth test case, for node $1$, the possible strings are:

翻译中…
  • aaaa (obtainable $1$ way)
  • aaa (obtainable $4$ ways)
  • aa (obtainable $6$ ways)
  • a (obtainable $4$ ways)
翻译中…

Therefore, the answer for node $1$ is $1^2+4^2+6^2+4^2=69$.

翻译中…

样例

Input #1
5
3
abb
1 2
1 3
2
aa
1 2
4
ccbb
1 2
2 3
2 4
4
aaaa
1 4
4 2
2 3
10
cacbcccbac
1 2
2 3
3 4
2 5
1 6
2 7
3 8
4 9
8 10
Output #1
9 1 1
5 1
29 9 1 1
69 5 1 19
185 65 19 3 1 1 1 3 1 1
InputCopy OutputCopy
题目信息
题号
2183F
所属比赛
时间限制
3 s
内存限制
1024 MB
标签
暴力
组合数学
DFS 及类似
动态规划
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。