This page is still under construction.

Parts of this page are still being built. What you see may change.

Nutella Tree (Easy)

Interview

Time limit2sMemory limit1024 MB

Summary
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 NN 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 [v1,v2,⋯ ⁣,vk][v_1, v_2, \cdots\!, v_k] satisfying all of the following conditions as a Nutella path.

  • kk is at least 22.
  • For each 1≤i≤k−11 \le i \le k-1, viv_i and vi+1v_{i+1} are directly connected by an edge in the tree.
  • v1v_1 is black.
  • For each 2≤i≤k2 \le i \le k, viv_i is red.

Find the total number of Nutella paths in the given tree.

Input

The first line gives the number of vertices NN of the tree. (2≤N≤100 0002 \le N \le 100\,000)

Over the following (N−1)(N-1) lines, the numbers uiu_i, viv_i of the two vertices connected by each edge are given, separated by spaces. (1≤ui≤N1 \le u_i \le N, 1≤vi≤N1 \le v_i \le N, ui≠viu_i \neq v_i)

The next line gives a string CC of length NN consisting only of the letters B and R. The ii-th character of CC represents the color of vertex ii, 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.

Examples1

  1. Example 1

    Input
    6
    1 3
    2 4
    5 3
    4 6
    3 4
    RRBRRB
    
    Expected output
    6