Contents
Bakry and Partitioning solution codeforces

For Solution
Click Here!
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 iith node has value aiai assigned to it for each ii from 11 to nn. As a reminder, a tree on nn nodes is a connected graph with n−1n−1 edges.
You want to delete at least 11, but at most k−1k−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. Bakry and Partitioning solution codeforces
Is it possible to achieve this condition?
Bakry and Partitioning solution codeforces
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5⋅104)(1≤t≤5⋅104). Description of the test cases follows.
The first line of each test case contains two integers nn and kk (2≤k≤n≤105)(2≤k≤n≤105).
The second line of each test case contains nn integers a1,a2,...,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109).
The iith of the next n−1n−1 lines contains two integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi), which means that there’s an edge between nodes uiui and vivi.
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 2⋅1052⋅105.
Bakry and Partitioning solution codeforces
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).
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
Bakry and Partitioning solution codeforces
NO YES NO YES NO
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)
Bakry and Partitioning solution codeforces
The bitwise XOR of the first component will be, a1⊕a2⊕a3⊕a4=1⊕6⊕4⊕1=2a1⊕a2⊕a3⊕a4=1⊕6⊕4⊕1=2 (where ⊕⊕ denotes the bitwise XOR).
The bitwise XOR of the second component will be, a5=2a5=2.

For Solution
Click Here!