CF BUDDY
← Problems·

1975E · Chain Queries

2100 · binary search, data structures, dfs and similar

Problem: You are given a tree of nn vertices numbered from 11 to nn. Initially, all vertices are colored white or black.

You are asked to perform qq queries:

  • "u" — toggle the color of vertex uu (if it was white, change it to black and vice versa).

After each query, you should answer whether all the black vertices form a chain. That is, there exist two black vertices such that the simple path between them passes through all the black vertices and only the black vertices. Specifically, if there is only one black vertex, they form a chain. If there are no black vertices, they do not form a chain.

Input Format: Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041\leq t\leq 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and qq (1n,q21051\leq n,q\leq 2\cdot 10^5).

The second line of each test case contains nn integers c1,c2,,cnc_1,c_2,\ldots,c_n (ci{0,1}c_i \in \{ \mathtt{0}, \mathtt{1} \}) — the initial color of the vertices. cic_i denotes the color of vertex ii where 0\mathtt{0} denotes the color white, and 1\mathtt{1} denotes the color black.

Then n1n - 1 lines follow, each line contains two integers xix_i and yiy_i (1xi,yin1 \le x_i,y_i \le n), indicating an edge between vertices xix_i and yiy_i. It is guaranteed that these edges form a tree.

The following qq lines each contain an integer uiu_i (1uin1 \le u_i \le n), indicating the color of vertex uiu_i needs to be toggled.

It is guaranteed that the sum of nn and qq over all test cases respectively does not exceed 21052\cdot 10^5.

Output Format: For each query, output "Yes" if the black vertices form a chain, and output "No" otherwise.

You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

Note: In the second test case, the color of the vertices are as follows:

The initial tree:

The first query toggles the color of vertex 44:

The second query toggles the color of vertex 33:

The third query toggles the color of vertex 22:

The fourth query toggles the color of vertex 55:

Sample Cases

Case 1

Input

2
2 1
1 0
1 2
1
5 4
1 0 0 0 0
1 2
1 3
1 5
3 4
4
3
2
5

Output

No
No
Yes
Yes
No

Case 2

Input

4
5 3
1 1 1 1 1
3 5
2 5
3 4
1 5
1
1
1
4 4
0 0 0 0
1 2
2 3
1 4
1
2
3
2
1 1
1
1
1 1
0
1

Output

Yes
No
Yes
Yes
Yes
Yes
No
No
Yes

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback