CF BUDDY
← Problems·

1592C · Bakry and Partitioning

1700 · bitmasks, constructive algorithms, dfs and similar

Problem: Bakry faced a problem, but since he's lazy to solve it, he asks for your help.

You are given a tree of nn nodes, the ii-th node has value aia_i assigned to it for each ii from 11 to nn. As a reminder, a tree on nn nodes is a connected graph with n1n-1 edges.

You want to delete at least 11, but at most k1k-1 edges from the tree, so that the following condition would hold:

  • For every connected component calculate the bitwise XOR of the values of the nodes in it. Then, these values have to be the same for all connected components.

Is it possible to achieve this condition?

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

The first line of each test case contains two integers nn and kk (2kn105)(2 \leq k \leq n \leq 10^5).

The second line of each test case contains nn integers a1,a2,...,ana_1, a_2, ..., a_n (1ai109)(1 \leq a_i \leq 10^9).

The ii-th of the next n1n-1 lines contains two integers uiu_i and viv_i (1ui,vin1 \leq u_i, v_i \leq n, uiviu_i\neq v_i), which means that there's an edge between nodes uiu_i and viv_i.

It is guaranteed that the given graph is a tree.

It is guaranteed that the sum of nn over all test cases doesn't exceed 21052 \cdot 10^5.

Output Format: For each test case, you should output a single string. If you can delete the edges according to the conditions written above, output "YES" (without quotes). Otherwise, output "NO" (without quotes).

You can print each letter of "YES" and "NO" in any case (upper or lower).

Note: It can be shown that the objection is not achievable for first, third, and fifth test cases.

In the second test case, you can just remove all the edges. There will be 55 connected components, each containing only one node with value 33, so the bitwise XORs will be 33 for all of them.

In the fourth test case, this is the tree: .

You can remove an edge (4,5)(4,5)

The bitwise XOR of the first component will be, a1a2a3a4=1641=2a_1 \oplus a_2 \oplus a_3 \oplus a_4 = 1 \oplus 6 \oplus 4 \oplus 1 = 2 (where \oplus denotes the bitwise XOR).

The bitwise XOR of the second component will be, a5=2a_5 = 2.

Sample Cases

Case 1

Input

5
2 2
1 3
1 2
5 5
3 3 3 3 3
1 2
2 3
1 4
4 5
5 2
1 7 2 3 5
1 2
2 3
1 4
4 5
5 3
1 6 4 1 2
1 2
2 3
1 4
4 5
3 3
1 7 4
1 2
2 3

Output

NO
YES
NO
YES
NO

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