Under construction

Still being built. Things may change

Black and White Landscaper

Interview

Time limit2sMemory limit1024 MB

Summary
Given a tree with vertices colored white or black, find every root for which each vertex with children takes the majority color of its descendants.
Level

Medium6 of 10

Topics
Tree, DFS, Dynamic programming
Solved
No attempts yet

Problem

Mina is a landscaper known for decorating gardens with Even trees. A tree is a simple connected graph without cycles. A tree is Even if it satisfies the following conditions.

  • It has one root vertex. Two vertices connected by an edge have a parent-child relationship, and the one closer to the root is the parent.
  • Every vertex is painted either white or black.
    • A vertex with no children may be any color.
    • A vertex with children must have the color that is more common among all descendants of that vertex. That is, it must be white if there are more white descendants, and black if there are more black descendants. If the numbers of white and black descendants are equal, it may be any color. Here, for two distinct vertices aa and bb, bb is an ancestor of aa if bb lies on the simple path from aa to the root vertex, and then aa is a descendant of bb.

Mina took a tree with NN vertices to decorate the HCPC contest venue. When she arrived, however, she forgot which vertex was the root. Depending on which vertex is chosen as the root, the tree may or may not be Even. Find every vertex that makes the tree Even when it is chosen as the root.

Input

The first line contains the number of vertices NN. (2≤N≤2000002 \le N \le 200000)

The second line contains NN integers C1,C2,⋯ ,CNC_1, C_2, \cdots, C_N separated by spaces. If vertex ii is white, Ci=0C_i=0. If it is black, Ci=1C_i=1.

Each of the next N−1N-1 lines contains two vertex numbers uu and vv separated by a space, meaning that an edge connects them. (1≤u,v≤N1 \le u,v \le N; u≠vu \ne v)

Output

On the first line, print the number of vertices that satisfy the condition.

On the second line, print the numbers of all vertices that satisfy the condition in ascending order, separated by spaces.

If no vertex satisfies the condition, do not print the second line.

Examples2

  1. Example 1

    Input
    10
    0 0 0 1 1 0 1 1 0 0
    6 1
    6 3
    6 5
    6 7
    6 10
    5 2
    5 4
    7 8
    7 9
    
    Expected output
    4
    1 3 6 10
    
  2. Example 2

    Input
    8
    1 1 1 0 0 1 0 0
    3 8
    7 1
    6 4
    5 7
    2 4
    8 2
    4 5
    
    Expected output
    0