You have a tree of
vertices rooted at vertex , where is even. The -th vertex has a binary value written on it (i.e, each is either or ).
A tree is said to be balanced if and only if the number of zeros written on its vertices equals the number of ones written on its vertices.
You can select some disjoint subtrees and flip the values on them (i.e, make all 0's into 1's and all 1's into 0's). Formally, you can pick any subset (possibly empty) of vertices such that is not an ancestor of for , and flip the value of for every vertex that is contained in the subtree of some .
Find a way of performing this operation that makes the entire tree balanced. It can be proved that the solution always exists.
If there are multiple possible solutions, you may print any of them. In particular, you do not need to minimize the number of subtrees that are flipped.
Input Format
- The first line of input contains a single integer , the number of test cases. The description of test cases follows.
- The first line of each test case contains an integer — the size of the tree.
- The second line contains space-separated integers — the values on the vertices.
- Then, lines follow. The -th line contains two space-separated integers and — the vertices connected by the -th edge.
Output Format
- For each test case, first print a single integer — the number of operations you performed.
- Then, on the second line, print space-separated integers — the roots of the subtrees you decided to flip.
- If there are multiple solutions, you can print any of them.
Constraints
- The sum of across all test cases doesn't exceed
- is even
Subtasks
- Subtask 1 (10 points):
- The -th edge connects vertices and .
- Subtask 2 (25 points):
- Sum of across all test cases doesn't exceed .
- Subtask 3 (30 points):
- for .
- Subtask 4 (35 points):
- No further constraints.
Sample Input 1
2
4
0 1 0 0
1 2
1 3
3 4
4
1 1 1 1
1 2
1 3
1 4
Sample Output 1
1
4
2
3 4
Explanation
Test case : The values become after the flip. The tree is balanced since there are two 0's and two 1's.
Test case : The values become after the flips.
No comments:
Post a Comment