CF1626E.Black and White Tree
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a tree consisting of n 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 x . You choose a black vertex y , and then move the chip along the first edge on the simple path from x to y .
You are not allowed to choose the same black vertex y 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 100500 .
For every vertex i , 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 i .
输入格式
The first line contains one integer n ( 3≤n≤3⋅105 ) — the number of vertices in the tree.
The second line contains n integers c1,c2,…,cn ( 0≤ci≤1 ), where ci=0 means that the i -th vertex is white, and ci=1 means that the i -th vertex is black. At least two values of ci are equal to 1 .
Then n−1 lines follow, each of them contains two integers ui and vi ( 1≤ui,vi≤n ; ui=vi ) — the endpoints of some edge. These edges form a tree.
输出格式
Print n integers. The i -th integer should be equal to 1 if there exists a (possibly empty) sequence of operations that moves the chip to some black vertex if it is placed on the vertex i , and 0 if no such sequence of operations exists.
输入输出样例
输入#1
8 0 1 0 0 0 0 1 0 8 6 2 5 7 8 6 5 4 5 6 1 7 3
输出#1
0 1 1 1 1 0 1 1