题库练习 Anji's Binary Tree
← 上一题 下一题 →

A16304 | Anji's Binary Tree

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

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 $n$ vertices. Vertex $1$ is the root and does not have a parent. All other vertices have exactly one parent. Each vertex can have up to $2$ 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 $s_i$ 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.

Before his journey, he can perform the following operations: choose any node, and replace the letter written on it with another one. 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.
凯克西奇一直被安吉冷落。通过一个共同的朋友,他发现安吉非常喜欢二叉树,于是决定解决她的问题,以引起她的注意。

Anji 给了 Keksic 一棵有 n
个顶点的二叉树。顶点 1
是根,没有父顶点。所有其他顶点都有一个父顶点。每个顶点最多可以有 2
个子顶点、一个左子顶点和一个右子顶点。对于每个顶点,安吉会告诉凯克西奇它的左子和右子的索引,或者告诉他它们不存在。

此外,每个顶点上都有一个字母 si
,分别是 "U"、"L "或 "R"。

凯克西奇从根部开始他的旅程,在每一步棋中他都会进行以下操作:

如果当前顶点上的字母是 "U",他就移动到它的父顶点。如果它不存在,他就什么也不做。
如果当前顶点上的字母是 "L",则移动到其左侧子顶点。如果它不存在,则他什么也不做。
如果当前顶点上的字母是 "R",则移动到其右边的子顶点。如果它不存在,则他什么也不做。
在移动之前,他可以执行以下操作:选择任意一个节点,并用另一个节点替换写在上面的字母。

我们感兴趣的是,当他开始旅行时,他将在某一点到达一片叶子,那么他在旅行前需要做的操作的最小数目。叶子是一个没有子顶点的顶点。他到达哪片叶子并不重要。需要注意的是,他是否会停留在叶子上并不重要,他只需要移动到叶子上。此外,他需要移动多少次才能到达一片叶子也无关紧要。

帮助 Keksic 解开安吉的树,这样他就能赢得她的芳心,让她来到恰恰克。

输入格式

Each test consists of multiple test cases. The first line contains a single integer $t$ ( $1 \le t \le 5 \cdot 10^4$ ) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer $n$ ( $2 \le n \le 3 \cdot 10^5$ ) — the number of vertices in a tree.

The second line of each test case contains a string $s$ of $n$ characters — characters are written on the vertices. It is guaranteed that $s$ consists only of characters 'U', 'L', and 'R'.

The $i$ -th of the next $n$ lines contains two integers $l_i$ and $r_i$ ( $0 \le l_i, r_i \le n$ ) — indices of left and right child of the vertex $i$ . If $l_i = 0$ , it means that vertex $i$ does not have a left child. If $r_i = 0$ , it means that vertex $i$ does not have a right child. It is guaranteed that this data describes a valid binary tree rooted at $1$ .

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$ .
输入

每个测试由多个测试用例组成。第一行包含一个整数 $t$ ( $1 \leq t \le 5 \cdot 10^4$ )--测试用例数。( $1 \le t \le 5 \cdot 10^4$ ) - 测试用例的数量。测试用例说明如下。

每个测试用例的第一行都包含一个整数 $n$ ( $2 \le n \le 3 \cdot 10^5$ ) - 树的顶点数。

每个测试用例的第二行包含一个由 $n$ 个字符组成的字符串 $s$ - 字符写在顶点上。保证 $s$ 只包含字符 "U"、"L "和 "R"。

接下来 $n$ 行的 $i-th$包含两个整数 $l _i$ 和 $r_i$ ( $0 \leq l_i, r_i \leq n$ )--顶点 $i$ 的左右子节点的索引。如果是 $l _ i = 0$ ,则表示顶点 $i$ 没有左子顶点。如果是 $r_i = 0$ ,则表示顶点 $i$ 没有右子顶点。可以保证,这些数据描述了一棵有效的二叉树,其根位于 $1$ 。

保证所有测试用例中 $n$ 的总和不超过 $3 \cdot 10^5$ 。

输出格式

For each test case, output a single integer — the minimal number of operations Keksic needs to do to reach a leaf.
输出

对于每个测试用例,输出一个整数,即 Keksic 到达叶子所需的最小操作数。

输入输出样例

输入 #1
5
3
LRU
2 3
0 0
0 0
3
ULR
3 2
0 0
0 0
2
LU
0 2
0 0
4
RULR
3 0
0 0
0 4
2 0
7
LLRRRLU
5 2
3 6
0 0
7 0
4 0
0 0
0 0
输出 #1
0
1
1
3
1
C++ 编辑器
输入
输出