#P7206. [COCI 2019/2020 #3] Lampice

[COCI 2019/2020 #3] Lampice

Description

Mirko decorates a Christmas tree with NN LED lights. Their colors are known, and they are connected by (N1)(N-1) wires.

After finishing, Mirko carefully admires his work. He is attracted by a special pattern called a "palindrome segment". A "palindrome segment" is a path from uu to vv such that the sequence of light colors along the path from uu to vv is the same as the sequence of light colors along the path from vv to uu.

Find the longest "palindrome segment" in the Christmas tree.

Input Format

The first line contains an integer NN, the number of LED lights.

The second line contains a string of NN lowercase English letters, where the ii-th letter represents the color of the ii-th light.

The next (N1)(N-1) lines each contain two integers A,BA, B, indicating that AA and BB are connected by a wire.

Output Format

Output the longest "palindrome segment" in the Christmas tree.

7
imanade
1 2
2 3
3 4
4 5
5 6
6 7
3
4
aabb
1 2
1 3
3 4
2
8
acdbabcd
1 6
6 7
6 3
3 4
4 5
5 2
8 5
5

Hint

Constraints

Subtask Score Constraints
11 1717 N3000N \le 3000
22 2525 The ii-th light is directly connected to the (i+1)(i+1)-th light (1i<N1 \le i \lt N)
33 3131 At most 100100 lights are directly connected to another light
44 3737 None

For 100%100\% of the testdata, $1 \le N \le 5 \times 10^4, 1 \le A, B \le N, A \neq B$.

Notes

The score of this problem is set according to the original COCI problem, with a full score of 110110.

Translated from COCI2019-2020 CONTEST #3 T4 Drvca.

Translated by ChatGPT 5