CF1771D.Hossam and (sub-)palindromic tree

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Hossam has an unweighted tree GG with letters in vertices.

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

A string aa is a subsequence of a string ss if aa can be obtained from ss 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 ss as a subsequence of ss, 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 ss as a sub-palindrome of ss, which has the maximal length among all sub-palindromes of ss. 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 44 maximum sub-palindromes.

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

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

霍桑有一棵无权树 GG,其顶点上标有字母。

霍桑将 s(v, u)s(v, \, u) 定义为:在树 GG 中,从顶点 vv 到顶点 uu 的唯一简单路径上所有顶点所标字母按顺序写出所构成的字符串。

若字符串 aa 可通过从字符串 ss 中删除若干(可能为零个)字母得到,则称 aa 是 ss 的一个子序列。例如,“dores”、“cf” 和 “for” 都是 “codeforces” 的子序列,而 “decor” 和 “fork” 则不是。

回文串(palindrome)是指正读与反读都相同的字符串。例如,“abacaba” 是回文串,但 “abac” 不是。

霍桑将字符串 ss 的一个子回文串(sub-palindrome)定义为:ss 的一个子序列,且该子序列本身是回文串。例如,“k”、“abba” 和 “abhba” 都是字符串 “abhbka” 的子回文串,而 “abka” 和 “cat” 则不是。

霍桑将字符串 ss 的一个最长子回文串(maximal sub-palindrome)定义为:ss 的一个子回文串,且其长度在 ss 的所有子回文串中达到最大。例如,“abhbka” 仅有一个最长子回文串——“abhba”。但字符串也可能存在多个长度相同的最长子回文串:例如,“abcd” 有 44 个最长子回文串。

请帮助霍桑找出:在树 GG 中所有 s(v, u)s(v, \, u) 对应的字符串中,其最长子回文串的最大长度。

注意:此处的子回文串指的是子序列,而非子串。

输入格式

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

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

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

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

It is guaranteed that sum of all nn doesn't exceed 2⋅1032 \cdot 10^3.

第一行包含一个整数 tt(1≤t≤2001 \le t \le 200)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1031 \le n \le 2 \cdot 10^3)—— 图中顶点的数量。

第二行包含一个长度为 nn 的字符串 ss,其中第 ii 个字符表示顶点 ii 上的字母。保证该字符串中的所有字符均为小写英文字母。

接下来的 n−1n - 1 行描述树的边。每条边由两个整数 vv 和 uu(1≤v, u≤n1 \le v, \, u \le n,且 v≠uv \neq u)给出。这两个数表示树中存在一条边 (v, u)(v, \, u)。保证所给的边构成一棵树。

保证所有 nn 的总和不超过 2⋅1032 \cdot 10^3。

输出格式

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

对于每个测试用例,输出一个整数——所有 s(v, u)s(v, \, u) 中最长的极大回文子串的长度。

输入输出样例

  • 输入#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

    输出#1

    3
    5

说明/提示

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

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, 94, \, 2, \, 1, \, 5, \, 9.

The tree from the second example.

在第一个例子中,最长回文子串为 “aaa”,对应顶点 1, 3, 51, \, 3, \, 5 上的字母;或 “aca”,对应顶点 1, 4, 51, \, 4, \, 5 上的字母。

第一个例子中的树。

在第二个例子中,仅存在一个最长回文子串 “bacab”,对应顶点 4, 2, 1, 5, 94, \, 2, \, 1, \, 5, \, 9 上的字母。

第二个例子中的树。

输入解题思路,AI测评打分。不知道怎么写?

首页