Codeforces Round 837 (Div. 2)
D
Hossam and (sub-)palindromic tree
Codeforces
2100
原题
← CE →
翻译中…

Hossam has an unweighted tree $G$ with letters in vertices.

翻译中…

Hossam defines $s(v, \, u)$ as a string that is obtained by writing down all the letters on the unique simple path from the vertex $v$ to the vertex $u$ in the tree $G$.

翻译中…

A string $a$ is a subsequence of a string $s$ if $a$ can be obtained from $s$ by deletion of several (possibly, zero) letters. For example, "dores", "cf", and "for" are subsequences of "codeforces", while "decor" and "fork" are not.

翻译中…

A palindrome is a string that reads the same from left to right and from right to left. For example, "abacaba" is a palindrome, but "abac" is not.

翻译中…

Hossam defines a sub-palindrome of a string $s$ as a subsequence of $s$, that is a palindrome. For example, "k", "abba" and "abhba" are sub-palindromes of the string "abhbka", but "abka" and "cat" are not.

翻译中…

Hossam defines a maximal sub-palindrome of a string $s$ as a sub-palindrome of $s$, which has the maximal length among all sub-palindromes of $s$. For example, "abhbka" has only one maximal sub-palindrome — "abhba". But it may also be that the string has several maximum sub-palindromes: the string "abcd" has $4$ maximum sub-palindromes.

翻译中…

Help Hossam find the length of the longest maximal sub-palindrome among all $s(v, \, u)$ in the tree $G$.

翻译中…

Note that the sub-palindrome is a subsequence, not a substring.

翻译中…

Input

翻译中…

The first line contains one integer $t$ ($1 \le t \le 200$) — the number of test cases.

翻译中…

The first line of each test case has one integer number $n$ ($1 \le n \le 2 \cdot 10^3$) — the number of vertices in the graph.

翻译中…

The second line contains a string $s$ of length $n$, the $i$-th symbol of which denotes the letter on the vertex $i$. It is guaranteed that all characters in this string are lowercase English letters.

翻译中…

The next $n - 1$ lines describe the edges of the tree. Each edge is given by two integers $v$ and $u$ ($1 \le v, \, u \le n$, $v \neq u$). These two numbers mean that there is an edge $(v, \, u)$ in the tree. It is guaranteed that the given edges form a tree.

翻译中…

It is guaranteed that sum of all $n$ doesn't exceed $2 \cdot 10^3$.

翻译中…

Output

翻译中…

For each test case output one integer — the length of the longest maximal sub-palindrome among all $s(v, \, u)$.

翻译中…

Note

翻译中…

In the first example the maximal subpalindromes are "aaa" with letters in vertices $1, \, 3, \, 5$, or "aca" with letters in vertices $1, \, 4, \, 5$.

翻译中…

image The tree from the first example.

翻译中…

In the second example there is only one maximal palindrome "bacab" with letters in vertices $4, \, 2, \, 1, \, 5, \, 9$.

翻译中…

image The tree from the second example.

翻译中…

样例

Input #1
2
5
abaca
1 2
1 3
3 4
4 5
9
caabadedb
1 2
2 3
2 4
1 5
5 6
5 7
5 8
8 9
Output #1
3
5
InputCopy OutputCopy
题目信息
题号
1771D
时间限制
1 s
内存限制
256 MB
标签
暴力
数据结构
DFS 及类似
动态规划
字符串
树

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