A14815 | Black and White Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $100^{500}$ .
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$ .
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 $100^{500}$ .
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 \le n \le 3 \cdot 10^5$ ) — the number of vertices in the tree.
The second line contains $n$ integers $c_1, c_2, \dots, c_n$ ( $0 \le c_i \le 1$ ), where $c_i = 0$ means that the $i$ -th vertex is white, and $c_i = 1$ means that the $i$ -th vertex is black. At least two values of $c_i$ are equal to $1$ .
Then $n-1$ lines follow, each of them contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le n$ ; $u_i \ne v_i$ ) — the endpoints of some edge. These edges form a tree.
The second line contains $n$ integers $c_1, c_2, \dots, c_n$ ( $0 \le c_i \le 1$ ), where $c_i = 0$ means that the $i$ -th vertex is white, and $c_i = 1$ means that the $i$ -th vertex is black. At least two values of $c_i$ are equal to $1$ .
Then $n-1$ lines follow, each of them contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le n$ ; $u_i \ne v_i$ ) — 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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted