Codeforces Round 969 (Div. 1)
A
Iris and Game on the Tree
Codeforces
1700
原题
←B →
翻译中…

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$.

翻译中…

image

翻译中…
  • Let's calculate the weight of the leaf $5$: the formed string is $\mathtt{10110}$. The number of occurrences of substring $\mathtt{10}$ is $2$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $2 - 1 = 1$.
  • Let's calculate the weight of the leaf $6$: the formed string is $\mathtt{101}$. The number of occurrences of substring $\mathtt{10}$ is $1$, the number of occurrences of substring $\mathtt{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 $\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.

翻译中…

Input

翻译中…

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$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the final score of the tree.

翻译中…

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 $\mathtt{01}$, so the weight of the leaf is $0-1=-1$.
  • From vertex $1$ to vertex $3$. The string formed by the path is $\mathtt{00}$, so the weight of the leaf is $0-0=0$.
  • From vertex $1$ to vertex $4$. The string formed by the path is $\mathtt{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$.

翻译中…

In the second test case, one of the sequences of optimal choices for the two players can be:

翻译中…
  • Iris chooses to change the value of the vertex $3$ to $\mathtt 1$.
  • Dora chooses to change the value of the vertex $1$ to $\mathtt 0$.
  • Iris chooses to change the value of the vertex $2$ to $\mathtt 0$.
翻译中…

The final tree is as follows:

翻译中…

image

翻译中…

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.

翻译中…

样例

Input #1
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
??
Output #1
2
1
1
2
1
0
InputCopy OutputCopy
题目信息
题号
2006A
时间限制
2 s
内存限制
256 MB
标签
构造
DFS 及类似
博弈论
图论
贪心
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。