#P11251. Longest Beautiful Path in a Colored Tree
Longest Beautiful Path in a Colored Tree
Longest Beautiful Path in a Colored Tree
You are given a tree with \( n \) nodes numbered from \( 1 \) to \( n \). Each node is colored either black or white. A simple path (a path that does not pass through any node more than once) in the tree is considered beautiful if for every pair of adjacent nodes on the path, their colors are different. For example, if node \(1\) and node \(4\) are black and all other nodes are white, then the path \(2-1-3-4\) is beautiful while the path \(2-1-3-5\) is not, because nodes \(3\) and \(5\) share the same color.
The length of a path is defined as the number of nodes it contains. Your task is to determine the length of the longest beautiful path in the tree.
Note: A single node is considered a valid beautiful path.
inputFormat
The input consists of multiple lines.
- The first line contains an integer \( n \) \( (1 \leq n \leq 10^5) \) representing the number of nodes in the tree.
- The second line contains a string of length \( n \) consisting only of the characters 'B' and 'W'. The \( i \)-th character denotes the color of node \( i \), where 'B' stands for black and 'W' stands for white.
- The following \( n-1 \) lines each contain two integers \( u \) and \( v \) \( (1 \leq u,v \leq n) \) representing an edge between nodes \( u \) and \( v \).
outputFormat
Output a single integer: the length of the longest beautiful path in the tree.
sample
5
BWWBW
2 1
1 3
3 4
3 5
4
</p>