#P1626E. Black and White Tree
Black and White Tree
No submission language available for this problem.
Description
You are given a tree consisting of vertices. Some of the vertices (at least two) are black, all the other vertices are white.
You place a chip on one of the vertices of the tree, and then perform the following operations:
- let the current vertex where the chip is located is . You choose a black vertex , and then move the chip along the first edge on the simple path from to .
You are not allowed to choose the same black vertex in two operations in a row (i. e., for every two consecutive operations, the chosen black vertex should be different).
You end your operations when the chip moves to the black vertex (if it is initially placed in a black vertex, you don't perform the operations at all), or when the number of performed operations exceeds .
For every vertex , you have to determine if there exists a (possibly empty) sequence of operations that moves the chip to some black vertex, if the chip is initially placed on the vertex .
The first line contains one integer () — the number of vertices in the tree.
The second line contains integers (), where means that the -th vertex is white, and means that the -th vertex is black. At least two values of are equal to .
Then lines follow, each of them contains two integers and (; ) — the endpoints of some edge. These edges form a tree.
Print integers. The -th integer should be equal to if there exists a (possibly empty) sequence of operations that moves the chip to some black vertex if it is placed on the vertex , and if no such sequence of operations exists.
Input
The first line contains one integer () — the number of vertices in the tree.
The second line contains integers (), where means that the -th vertex is white, and means that the -th vertex is black. At least two values of are equal to .
Then lines follow, each of them contains two integers and (; ) — the endpoints of some edge. These edges form a tree.
Output
Print integers. The -th integer should be equal to if there exists a (possibly empty) sequence of operations that moves the chip to some black vertex if it is placed on the vertex , and if no such sequence of operations exists.