#P1900C. Anji's Binary Tree
Anji's Binary Tree
Description
Keksic keeps getting left on seen by Anji. Through a mutual friend, he's figured out that Anji really likes binary trees and decided to solve her problem in order to get her attention.
Anji has given Keksic a binary tree with vertices. Vertex is the root and does not have a parent. All other vertices have exactly one parent. Each vertex can have up to children, a left child, and a right child. For each vertex, Anji tells Keksic index of both its left and its right child or tells him that they do not exist.
Additionally, each of the vertices has a letter on it, which is either 'U', 'L' or 'R'.
Keksic begins his journey on the root, and in each move he does the following:
- If the letter on his current vertex is 'U', he moves to its parent. If it doesn't exist, he does nothing.
- If the letter on his current vertex is 'L', he moves to its left child. If it doesn't exist, he does nothing.
- If the letter on his current vertex is 'R', he moves to its right child. If it doesn't exist, he does nothing.
You are interested in the minimal number of operations he needs to do before his journey, such that when he starts his journey, he will reach a leaf at some point. A leaf is a vertex that has no children. It does not matter which leaf he reaches. Note that it does not matter whether he will stay in the leaf, he just needs to move to it. Additionally, note that it does not matter how many times he needs to move before reaching a leaf.
Help Keksic solve Anji's tree so that he can win her heart, and make her come to Čačak.
题面翻译
给你一个二叉树,有人想从 节点开始走到叶子节点,行动方法如下:
- 当前点类型为 ,走到左儿子;
- 当前点类型为 ,走到右儿子;
- 当前点类型为 ,走到父亲。
行动前你可以修改点的类型,问至少需要修改多少次才能使得这个人能走到叶节点。
Input
Each test consists of multiple test cases. The first line contains a single integer () — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer () — the number of vertices in a tree.
The second line of each test case contains a string of characters — characters are written on the vertices. It is guaranteed that consists only of characters 'U', 'L', and 'R'.
The -th of the next lines contains two integers and () — indices of left and right child of the vertex . If , it means that vertex does not have a left child. If , it means that vertex does not have a right child. It is guaranteed that this data describes a valid binary tree rooted at .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the minimal number of operations Keksic needs to do to reach a leaf.
Note
In the first test case, vertex has as its left child and as its right child. Vertices and do not have children and are therefore leaves. As 'L' is written on vertex , Keksic will go to vertex , therefore he has to do no operations.
In the second test case, vertex has as its left child and as its right child. Vertices and are leaves. As 'U' is written on vertex , Keksic needs to change it to either 'L' or 'R' in order for him to reach a leaf.
In the third case, vertex has only a right child, which is vertex . As 'L' is written on it, Keksic needs to change it to 'R', otherwise he would be stuck on vertex .
In the fourth case, he can change characters so that letters on the vertices are "LURL", which makes him reach vertex .
In the fifth case, there are leaves, , and . To reach either leaf or leaf , he needs to change characters. However, if he changes character on vertex to 'R', he will reach leaf , therefore the answer is .

相关
在下列比赛中: