Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2)
G
Query Jungle
Codeforces
2900
原题
← FH1 →
翻译中…

Oner is a jungler — a role where you hunt monsters in a jungle. Given the number of trees he sees in a jungle, it's no surprise that he is addicted to tree query problems.

翻译中…

You are given a tree of $n$ vertices, rooted at vertex $1$. Each vertex either contains a monster or does not.

翻译中…

You want to find the minimum integer $k$ such that there exist $k$ paths that satisfy the following conditions:

翻译中…
  • Each path must start at the root (vertex $1$).
  • Every vertex with a monster must be included in at least one of these paths. A vertex is considered included in a path if it is one of the path's vertices, including its endpoints.
翻译中…

To make this problem more challenging, you must also answer $q$ queries. For each query, you are given a vertex $v$. For each vertex $w$ in the subtree of $v$, its status is inverted — the one containing a monster starts to not contain one, and the one not containing a monster starts to contain one. After each query, you must solve the original problem again with the updated status.

翻译中…

Note that queries are cumulative, so the effects of each query carry on to future queries.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 20\,000$). The description of the test cases follows.

翻译中…

The first line contains a single integer $n$ ($2 \le n \le 250\,000$) — the number of vertices in the tree.

翻译中…

The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i \in \{0, 1\}$), representing the initial status. If $a_i = 1$, vertex $i$ contains a monster; if $a_i = 0$, it does not.

翻译中…

The next $n-1$ lines each contain two integers $u$ and $v$ ($1 \le u, v \le n, u \ne v$), describing an edge between vertices $u$ and $v$. It is guaranteed that these edges form a tree.

翻译中…

The next line contains a single integer $q$ ($0 \le q \le 250\,000$) — the number of queries.

翻译中…

The next $q$ lines each contain a single integer $v_i$ ($1 \le v_i \le n$) — the vertex given for the $i$-th query.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $250\,000$.

翻译中…

It is guaranteed that the sum of $q$ over all test cases does not exceed $250\,000$.

翻译中…

Output

翻译中…

Print $q + 1$ lines. The first line should contain the minimum number of paths $k$ for the initial status. Each subsequent line should contain the answer after each query.

翻译中…

Note

翻译中…

Test Case 1:

翻译中…

Initial State: The monsters are in vertex $\{2, 4, 5\}$. We need two paths: $1 \to 7 \to 3 \to 2$ and $1 \to 7 \to 5 \to 4$. The answer is $2$.

翻译中…

After Query 1 ($v=2$): The monsters are in vertex $\{4, 5\}$. We only need one path, $1 \to 7 \to 5 \to 4$. The answer is $1$.

翻译中…

After Query 2 ($v=4$): The monsters are in vertex $\{5\}$. We only need one path, $1 \to 7 \to 5$. The answer is $1$.

翻译中…

After Query 3 ($v=6$): The monsters are in vertex $\{5, 6\}$. We need two paths, $1 \to 7 \to 5$ and $1 \to 6$. The answer is $2$.

翻译中…

After Query 4 ($v=7$): The monsters are in vertex $\{2, 3, 4, 6, 7\}$. We need three paths, $1 \to 6$, $1 \to 7 \to 5 \to 4$, and $1 \to 7 \to 3 \to 2$. The answer is $3$.

翻译中…

The following figure denotes the tree in the example input.

翻译中…

image

翻译中…

Test Case 2:

翻译中…

Initial State: The monsters are in vertex $\{2\}$. We need one path: $1 \to 2$. The answer is $1$.

翻译中…

After Query 1 ($v=2$): There are no monsters. We need zero paths. The answer is $0$.

翻译中…

After Query 2 ($v=1$): The monsters are in vertex $\{1,2\}$. We need one path: $1 \to 2$. The answer is $1$.

翻译中…

样例

Input #1
2
7
0 1 0 1 1 0 0
1 6
1 7
7 3
3 2
7 5
5 4
4
2
4
6
7
2
0 1
1 2
2
2
1
Output #1
2
1
1
2
3
1
0
1
InputCopy OutputCopy
题目信息
题号
2152G
时间限制
3 s
内存限制
1024 MB
标签
数据结构
模拟
数学
矩阵
树

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