Nutella Tree (Easy)
InterviewTime limit2sMemory limit1024 MB
Count paths in a red/black tree that start at a black vertex and continue through only red vertices, with at least two vertices.
- Level
Medium5 of 10
- Topics
- Tree, DFS, Implementation, Combinatorics
- Solved
- No attempts yet
Problem
Minje has a tree with vertices. Each vertex of this tree is colored either red or black.
Looking at this tree full of red and black vertices, Minje thought of Nutella. Nutella is Minje's favorite chocolate jam, and its logo looks like the following. Note that the first letter is black and the remaining letters are red.

Minje wonders how many Nutella logos can be found in the tree.
Define a sequence of distinct vertices satisfying all of the following conditions as a Nutella path.
- is at least .
- For each , and are directly connected by an edge in the tree.
- is black.
- For each , is red.
Find the total number of Nutella paths in the given tree.
Input
The first line gives the number of vertices of the tree. ()
Over the following lines, the numbers , of the two vertices connected by each edge are given, separated by spaces. (, , )
The next line gives a string of length consisting only of the letters B and R. The -th character of represents the color of vertex , where B means black and R means red.
Output
Print the number of Nutella paths on the first line.
Hint
The tree given as the example is drawn as follows.
