White-Black Balanced Subtrees Codeforces Solution | Codeforces Problem Solution 2022
You are given a rooted tree consisting of vertices numbered from to . The root is vertex . There is also a string denoting the color of each vertex: if , then vertex is black, and if , then vertex is white.
A subtree of the tree is called balanced if the number of white vertices equals the number of black vertices. Count the number of balanced subtrees.
A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. In this problem, all trees have root .
The tree is specified by an array of parents containing numbers: is the parent of the vertex with the number for all . The parent of a vertex is a vertex that is the next vertex on a simple path from to the root.
The subtree of a vertex is the set of all vertices that pass through on a simple path to the root. For example, in the picture below, is in the subtree of because the simple path passes through . Note that a vertex is included in its subtree, and the subtree of the root is the entire tree.
The first line of input contains an integer () — the number of test cases.
The first line of each test case contains an integer () — the number of vertices in the tree.
The second line of each test case contains integers () — the parents of the vertices .
The third line of each test case contains a string of length consisting of the characters and — the coloring of the tree.
Most Similar Words Codeforces Solution
It is guaranteed that the sum of the values over all test cases does not exceed .
For each test case, output a single integer — the number of balanced subtrees.
Join Now for Solution:-
No comments:
Post a Comment