CF1626E.Black and White Tree

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree consisting of nn 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 xx. You choose a black vertex yy, and then move the chip along the first edge on the simple path from xx to yy.

You are not allowed to choose the same black vertex yy 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 100500100^{500}.

For every vertex ii, 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 ii.

给你一棵包含 nn 个顶点的树。其中部分顶点(至少两个)为黑色,其余顶点均为白色。

你将一个棋子放置在树的某个顶点上,然后执行如下操作:

  • 设当前棋子所在顶点为 xx。你选择一个黑色顶点 yy,然后将棋子沿着从 xx 到 yy 的简单路径上的第一条边移动。

不允许在连续两次操作中选择同一个黑色顶点 yy(即:任意两次连续操作所选的黑色顶点必须不同)。

当棋子移动到某个黑色顶点时,你结束操作(如果初始时棋子已位于黑色顶点,则不执行任何操作);或者当已执行的操作次数超过 100500100^{500} 时,你也结束操作。

对每个顶点 ii,你需要判断:若棋子初始时置于顶点 ii,是否存在一个(可能为空的)操作序列,使得棋子最终移动到某个黑色顶点。

输入格式

The first line contains one integer nn (3≤n≤3⋅1053 \le n \le 3 \cdot 10^5) — the number of vertices in the tree.

The second line contains nn integers c1,c2,…,cnc_1, c_2, \dots, c_n (0≤ci≤10 \le c_i \le 1), where ci=0c_i = 0 means that the ii-th vertex is white, and ci=1c_i = 1 means that the ii-th vertex is black. At least two values of cic_i are equal to 11.

Then n−1n-1 lines follow, each of them contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n; ui≠viu_i \ne v_i) — the endpoints of some edge. These edges form a tree.

第一行包含一个整数 nn(3≤n≤3⋅1053 \le n \le 3 \cdot 10^5)—— 树中顶点的数量。

第二行包含 nn 个整数 c1,c2,…,cnc_1, c_2, \dots, c_n(0≤ci≤10 \le c_i \le 1),其中 ci=0c_i = 0 表示第 ii 个顶点为白色,ci=1c_i = 1 表示第 ii 个顶点为黑色。至少有两个 cic_i 的值等于 11。

接下来是 n−1n-1 行,每行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n;ui≠viu_i \ne v_i)—— 某条边的两个端点。这些边构成一棵树。

输出格式

Print nn integers. The ii-th integer should be equal to 11 if there exists a (possibly empty) sequence of operations that moves the chip to some black vertex if it is placed on the vertex ii, and 00 if no such sequence of operations exists.

输出 nn 个整数。其中第 ii 个整数应为 11,当且仅当存在一个(可能为空的)操作序列,使得若棋子初始位于顶点 ii 上,则该序列可将其移动至某个黑色顶点;否则(即不存在这样的操作序列)该整数为 00。

输入输出样例

  • 输入#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

输入解题思路,AI测评打分。不知道怎么写?

首页