Query Jungle

시간 제한3초메모리 제한2048 MB

요약
뿌리 있는 트리에서 일부 정점에 몬스터가 있고, 각 서브트리 뒤집기 질의 후 모든 몬스터를 덮는 뿌리 시작 경로의 최소 개수를 구한다. The answer for a set of marked vertices is the count of marked vertices whose parent is not marked. A subtree flip at v toggles this count for v and all its children. So maintain for each vertex a value d(u) = a[u] AND (1 - a[parent(u)]), where a[1] is treated as 1 for the root's contribution. The answer is the sum of d(u) over all u. Under a flip of subtree(v), a[v] toggles, a[parent(v)] toggles (if v is not root), and for every child c of v, a[parent(c)] = a[v] toggles. So d(v) toggles value, d(c) for each child togg
난이도

어려움10점 중 8점

유형
트리, DFS, 누적 합, 구현
정답자
아직 제출이 없습니다

문제

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 nn vertices, rooted at vertex 11. Each vertex either contains a monster or does not.

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

  • Each path must start at the root (vertex 11).
  • 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 qq queries. For each query, you are given a vertex vv. For each vertex ww in the subtree of vv, 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.

입력

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

The first line contains a single integer nn (2≤n≤250,0002 \le n \le 250\\,000) --- the number of vertices in the tree.

The next line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (a_i∈0,1a\_i \in \\{0, 1\\}), representing the initial status. If a_i=1a\_i = 1, vertex ii contains a monster; if a_i=0a\_i = 0, it does not.

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

The next line contains a single integer qq (0≤q≤250,0000 \le q \le 250\\,000) --- the number of queries.

The next qq lines each contain a single integer v_iv\_i (1≤v_i≤n1 \le v\_i \le n) --- the vertex given for the ii-th query.

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

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

출력

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

힌트

Test Case 1:

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

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

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

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

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

The following figure denotes the tree in the example input.

Test Case 2:

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

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

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

예제1

  1. 예제 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
    
    예상 출력
    2
    1
    1
    2
    3
    1
    0
    1