Codeforces Round 969 (Div. 1) |
---|
Finished |
Iris has a tree rooted at vertex $$$1$$$. Each vertex has a value of $$$\mathtt 0$$$ or $$$\mathtt 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 $$$\mathtt{10}$$$ and $$$\mathtt{01}$$$ substrings in it.
Take the following tree as an example. Green vertices have a value of $$$\mathtt 1$$$ while white vertices have a value of $$$\mathtt 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 $$$\texttt{?}$$$. 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 $$$\texttt{?}$$$ and changes its value to $$$\mathtt{0}$$$ or $$$\mathtt{1}$$$, with Iris going first. The game continues until there are no vertices with value $$$\mathtt{?}$$$ 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.
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 5\cdot 10^4$$$) — 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 \leq n \leq 10^5$$$) — the number of vertices in the tree.
The following $$$n - 1$$$ lines each contain two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq 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 $$$\mathtt{0}$$$, $$$\mathtt{1}$$$ and $$$\mathtt{?}$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2\cdot 10^5$$$.
For each test case, output a single integer — the final score of the tree.
641 21 34 1010141 23 22 4???051 21 32 42 5?1?0161 22 33 45 33 6?0????51 21 31 41 511?1?22 1??
2 1 1 2 1 0
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:
Thus, there are two leaves with non-zero weight, so the score of the tree is $$$2$$$.
In the second test case, one of the sequences of optimal choices for the two players can be:
The final tree is as follows:
The only leaf with non-zero weight is $$$3$$$, so the score of the tree is $$$1$$$. Note that this may not be the only sequence of optimal choices for Iris and Dora.
Name |
---|