Under construction

Still being built. Things may change

Candelabra

Time limit5sMemory limit1024 MB

Summary
Given a tree of n vertices labeled with letters a to t, find the longest path subsequence with no palindromic substring of length 2 or more.
Level

Hard9 of 10

Topics
Divide and conquer, Tree, Dynamic programming, Bit manipulation
Solved
No attempts yet

Statement

A terrible Monster lives in a cursed castle hidden deep in a dark forest. Only a beautiful girl who loves him as he is can melt the ice in his heart and restore his human form.

Breaking the curse is not easy. The girl must find the longest melodic subsequence written on the vertices of a candelabra and recite it under the full moon.

The candelabra is a connected graph with nn vertices and n−1n-1 edges. Each vertex has a lowercase Latin letter from 'a' to 't' written on it.

A path is a sequence of vertices in which each vertex appears at most once and every two adjacent vertices are connected by an edge. A subsequence is a path with some elements removed, keeping the order of the remaining elements. A substring is a subsequence with zero or more vertices removed from the beginning and the end. A subsequence is called melodic if it has no substring of length greater than one that equals its reverse.

Input

The first line contains nn (2≤n≤5⋅1042\le n\le 5\cdot 10^4), the number of vertices. Each of the next n−1n-1 lines contains two integers xix_i and yiy_i (1≤xi,yi≤n1\le x_i,y_i \le n, xi≠yix_i \neq y_i), the vertices joined by an edge. There is exactly one path between any two vertices. The last line contains a string of nn lowercase letters from 'a' to 't', where the ii-th letter is written on vertex ii.

Output

Print one integer, the maximum length of a melodic subsequence.

Examples1

  1. Example 1

    Input
    5
    1 2
    4 1
    4 5
    2 3
    acbcb
    
    Expected output
    4