give optimum cpp solution for : Iris and Game on the Tree time limit per test2 seconds…
- User
give optimum cpp solution for : Iris and Game on the Tree time limit per test2 seconds memory limit per test256 megabytes Iris has a tree rooted at vertex 1 . Each vertex has a value of 0 or 1 .
Let's consider a leaf of the tree (the vertex 1 is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the weight of the leaf is the difference between the number of occurrences of 10 and 01 substrings in it.
Take the following tree as an example. Green vertices have a value of 1 while white vertices have a value of 0 .
Let's calculate the weight of the leaf 5 : the formed string is 10110 . The number of occurrences of substring 10 is 2 , the number of occurrences of substring 01 is 1 , so the difference is 2−1=1 . Let's calculate the weight of the leaf 6 : the formed string is 101 . The number of occurrences of substring 10 is 1 , the number of occurrences of substring 01 is 1 , so the difference is 1−1=0 . The score of a tree is defined as the number of leaves with non-zero weight in the tree.
But the values of some vertices haven't been decided and will be given to you as ? . Filling the blanks would be so boring, so Iris is going to invite Dora to play a game. On each turn, one of the girls chooses any of the remaining vertices with value ? and changes its value to 0 or 1 , with Iris going first. The game continues until there are no vertices with value ? left in the tree. Iris aims to maximize the score of the tree, while Dora aims to minimize that.
Assuming that both girls play optimally, please determine the final score of the tree.
Input Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤5⋅104 ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤105 ) — the number of vertices in the tree.
The following n−1 lines each contain two integers u and v (1≤u,v≤n ) — denoting an edge between vertices u and v .
It's guaranteed that the given edges form a tree.
The last line contains a string s of length n . The i -th character of s represents the value of vertex i . It's guaranteed that s only contains characters 0 , 1 and ? .
It is guaranteed that the sum of n over all test cases doesn't exceed 2⋅105 .
Output For each test case, output a single integer — the final score of the tree.
Example InputCopy 6 4 1 2 1 3 4 1 0101 4 1 2 3 2 2 4 ???0 5 1 2 1 3 2 4 2 5 ?1?01 6 1 2 2 3 3 4 5 3 3 6 ?0???? 5 1 2 1 3 1 4 1 5 11?1? 2 2 1 ?? OutputCopy 2 1 1 2 1 0 Note In the first test case, all the values of the vertices have been determined. There are three different paths from the root to a leaf:
From vertex 1 to vertex 2 . The string formed by the path is 01 , so the weight of the leaf is 0−1=−1 . From vertex 1 to vertex 3 . The string formed by the path is 00 , so the weight of the leaf is 0−0=0 . From vertex 1 to vertex 4 . The string formed by the path is 01 , so the weight of the leaf is 0−1=−1 . Thus, there are two leaves with non-zero weight, so the score of the tree is 2 .
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.