Black and White Landscaper
InterviewTime limit2sMemory limit1024 MB
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 and , is an ancestor of if lies on the simple path from to the root vertex, and then is a descendant of .
Mina took a tree with 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 . ()
The second line contains integers separated by spaces. If vertex is white, . If it is black, .
Each of the next lines contains two vertex numbers and separated by a space, meaning that an edge connects them. (; )
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.