Lampice
Time limit5sMemory limit512 MB
Given a tree whose nodes are colored letters, find the longest path whose color sequence reads the same in both directions, and report its number of nodes.
- Level
Medium7 of 10
- Topics
- Tree, Divide and conquer, String matching
- Solved
- No attempts yet
Problem
Mirko chose a Christmas tree for the upcoming holidays and decided to decorate it with Christmas lights. The lights consist of LED lights connected by conductive wires, so that all of the lights are connected. Each light has a color.
After he decorated the tree, Mirko proudly stared at his masterpiece. After a while, he started noticing different patterns. Among those patterns, he was particularly amazed by so-called palindromic segments. A palindromic segment is a segment of lights on the path between two fixed lights and such that the array of colors on the path from to is exactly the same as the array of colors on the path from to . Determine the length of the longest palindromic segment, expressed as the number of lights on that segment.
Input
The first line contains an integer ().
The next line contains an array of lowercase letters from the English alphabet, where the -th letter is the color of the -th light.
Each of the next lines contains two integers and (, ), which means that lights and are directly connected by a wire.
Output
The first line of output contains the length of the longest palindromic segment.