#P13954. [ICPC 2023 Nanjing R] 红黑树
[ICPC 2023 Nanjing R] 红黑树
Description
There is a rooted tree with vertices numbered from to , where vertex is the root. Each vertex has a color, which is either red or black.
We say a vertex is good, if every simple path from that vertex to any of its descendant leaf vertex contains the same number of black vertices. We say a tree is perfect, if all its vertices are good.
Let be the subtree rooted at vertex . For each , answer the following query: If you can choose a set of vertices and change their colors (that is, change red vertices to black, and change black vertices to red), calculate the minimum number of vertices you have to choose to make perfect.
Recall that a simple path is a path which does not go through the same edge multiple times.
Also recall that a subtree rooted at vertex is a tree consisting of all descendants of vertex and has vertex as the root. Note that any vertex is a descendant of itself.
Input Format
There are multiple test cases. The first line of the input contains an integer indicating the number of test cases. For each test case:
The first line contains an integer () indicating the number of vertices in the tree.
The second line contains a string of length (). If then vertex is red; If then vertex is black.
The third line contains integers () where is the parent of vertex .
It's guaranteed that the sum of of all test cases will not exceed .
Output Format
For each test case output one line containing integers separated by a space, where the -th integer indicates the minimum number of vertices whose color you have to change to make perfect.
Please, DO NOT output extra spaces at the end of each line, or your solution may be considered incorrect!
2
9
101011110
1 1 3 3 3 6 2 2
4
1011
1 1 3
4 1 2 0 0 0 0 0 0
2 0 0 0
Hint
:::align{center}
:::
We illustrate the first sample test case.
To make perfect, we can change the color of vertices , , and . After that, all paths from vertex to its descendant leaves will contain black vertices, all paths from vertex to its descendant leaves will contain black vertices, and all paths from vertex to its descendant leaves will contain black vertices. As vertices to only has one descendant leaf, they're always good.
To make perfect, we can change the color of vertex . After that, all paths from vertex to its descendant leaves will contain vertices. As vertices and only has one descendant leaf, they're always good.
京公网安备 11011102002149号