cswork / OAOpen practice workspace

MicrosoftOA #34

Longest Path With Different Adjacent Characters

Estimated HardJudge-ready

Topics (AI-labelled)Tree PathsDP Basics

A tree is rooted at 0, and each node carries a lowercase letter. Return the maximum number of nodes on any simple path in which every pair of adjacent nodes has different characters.

Input

This site's ranges: the first line contains N (1..100000), the second line contains N parent values (parent[0]=−1; the others are 0-based parent indices), and the third line contains N lowercase letters. The input is guaranteed to be a valid tree.

Output

Output a single integer, the answer.

Example 1

Input:

6
-1 0 0 1 1 2
abacbe

Output:

3

Example 2

Input:

4
-1 0 0 0
aabc

Output:

3

Example 3

Input:

1
-1
z

Output:

1

Example notes

Example 1: 0—1—3 has distinct characters a, b, c, so the longest path has 3 nodes.

Example 2: 2—0—3 with b, a, c forms a path of length 3.

Example 3: there is only one node, so the answer is 1.

Editorial

Loading solution…