Codeforces Round 911 C. Anji‘s Binary Tree

原题:
C. Anji’s Binary Tree
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 s_i si 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.
Input

Each test consists of multiple test cases. The first line contains a single integer t
(1≤t≤ 5 ⋅ 1 0 4 5⋅10^4 5104) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n
(2≤n≤ 3 ⋅ 1 0 5 3⋅10^5 3105) — the number of vertices in a tree.

The second line of each test case contains a string s
of

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值