Sunday, May 15, 2022

Balanced 0-1 Tree

 You have a tree of 

N vertices rooted at vertex 1, where N is even. The i-th vertex has a binary value Ai written on it (i.e, each Ai is either 0 or 1).

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 {v1,v2,,vk} such that vi is not an ancestor of vj for ij, and flip the value of Ax for every vertex x that is contained in the subtree of some vi.

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 T, the number of test cases. The description of test cases follows.
  • The first line of each test case contains an integer N — the size of the tree.
  • The second line contains N space-separated integers A1,A2,,AN — the values on the vertices.
  • Then, N1 lines follow. The i-th line contains two space-separated integers ui and vi — the vertices connected by the i-th edge.

Output Format

  • For each test case, first print a single integer m — the number of operations you performed.
  • Then, on the second line, print m space-separated integers — the roots of the subtrees you decided to flip.
  • If there are multiple solutions, you can print any of them.

Constraints

  • 1T3000
  • The sum of N across all test cases doesn't exceed 3105
  • N is even
  • 0Ai1

Subtasks

  • Subtask 1 (10 points):
    • The i-th edge connects vertices i and i+1.
  • Subtask 2 (25 points):
    • Sum of N across all test cases doesn't exceed 500.
  • Subtask 3 (30 points):
    • Ai=1 for 1iN.
  • 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 1: The values A1,A2,A3,A4 become [0,1,0,1] after the flip. The tree is balanced since there are two 0's and two 1's.

Test case 2: The values A1,A2,A3,A4 become [0,0,1,1] after the flips.

No comments:

Post a Comment

मुझे दिल में बसाना आसान नहीं....

पता है Recently i realised की मुझे दिल में बसाना आसान नहीं है बात-बात पे चिढ़ जाती हूं मैं दिल लगा लूं एक बार तो फिर ज़िद पे अड़...