codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1900C.md (4360B)


      1 Source: https://codeforces.com/problemset/problem/1900/C
      2 
      3 C. Anji's Binary Tree
      4 
      5 time limit per test
      6 
      7 2.5 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 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.
     24 
     25 Additionally, each of the vertices has a letter $s_i$ on it, which is either 'U', 'L' or 'R'.
     26 
     27 Keksic begins his journey on the root, and in each move he does the following:
     28 
     29 * If the letter on his current vertex is 'U', he moves to its parent. If it doesn't exist, he does nothing.
     30 * If the letter on his current vertex is 'L', he moves to its left child. If it doesn't exist, he does nothing.
     31 * If the letter on his current vertex is 'R', he moves to its right child. If it doesn't exist, he does nothing.
     32 
     33 Before his journey, he can perform the following operations: choose any node, and replace the letter written on it with another one.
     34 
     35 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.
     36 
     37 Help Keksic solve Anji's tree so that he can win her heart, and make her come to Čačak.
     38 
     39 Input
     40 
     41 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.
     42 
     43 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.
     44 
     45 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'.
     46 
     47 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$.
     48 
     49 It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
     50 
     51 Output
     52 
     53 For each test case, output a single integer — the minimal number of operations Keksic needs to do to reach a leaf.
     54 
     55 Example
     56 
     57 Input
     58 
     59 ```
     60 5
     61 
     62 3
     63 
     64 LRU
     65 
     66 2 3
     67 
     68 0 0
     69 
     70 0 0
     71 
     72 3
     73 
     74 ULR
     75 
     76 3 2
     77 
     78 0 0
     79 
     80 0 0
     81 
     82 2
     83 
     84 LU
     85 
     86 0 2
     87 
     88 0 0
     89 
     90 4
     91 
     92 RULR
     93 
     94 3 0
     95 
     96 0 0
     97 
     98 0 4
     99 
    100 2 0
    101 
    102 7
    103 
    104 LLRRRLU
    105 
    106 5 2
    107 
    108 3 6
    109 
    110 0 0
    111 
    112 7 0
    113 
    114 4 0
    115 
    116 0 0
    117 
    118 0 0
    119 ```
    120 
    121 Output
    122 
    123 ```
    124 0
    125 1
    126 1
    127 3
    128 1
    129 ```
    130 
    131 Note
    132 
    133 In the first test case, vertex $1$ has $2$ as its left child and $3$ as its right child. Vertices $2$ and $3$ do not have children and are therefore leaves. As 'L' is written on vertex $1$, Keksic will go to vertex $2$, therefore he has to do no operations.
    134 
    135 In the second test case, vertex $1$ has $3$ as its left child and $2$ as its right child. Vertices $2$ and $3$ are leaves. As 'U' is written on vertex $1$, Keksic needs to change it to either 'L' or 'R' in order for him to reach a leaf.
    136 
    137 In the third case, vertex $1$ has only a right child, which is vertex $2$. As 'L' is written on it, Keksic needs to change it to 'R', otherwise he would be stuck on vertex $1$.
    138 
    139 In the fourth case, he can change $3$ characters so that letters on the vertices are "LURL", which makes him reach vertex $2$.
    140 
    141 In the fifth case, there are $3$ leaves, $3$, $6$ and $7$. To reach either leaf $6$ or leaf $7$, he needs to change $2$ characters. However, if he changes character on vertex $1$ to 'R', he will reach leaf $3$, therefore the answer is $1$.
    142 
    143  ![](https://espresso.codeforces.com/1e67bb5647fb5e25676addfa3412a2a9c8b1a705.png) The initial tree in test case 5.