309273: CF1654G. Snowy Mountain
Memory Limit:1024 MB
Time Limit:5 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Snowy Mountain
题意翻译
* 给定一棵 $n$ 个点的树,其中每个点可能是黑色或白色。 * 一个点的高度定义为其距离最近黑色节点的距离。 * 你初始在 $i$ 号节点上,势能为 $0$,可以做以下两种操作: * 向高度更小的相邻节点移动,增加 $1$ 的势能。 * 向高度相同的相邻节点移动,减少 $1$ 的势能,这个操作只能在你的势能 $\geq 1$ 时执行。 * 对于 $i=1,2,\cdots, n$,求出你能做的操作数的最大值。 * $n\leq 2\times 10^5$。题目描述
There are $ n $ locations on a snowy mountain range (numbered from $ 1 $ to $ n $ ), connected by $ n-1 $ trails in the shape of a tree. Each trail has length $ 1 $ . Some of the locations are base lodges. The height $ h_i $ of each location is equal to the distance to the nearest base lodge (a base lodge has height $ 0 $ ). There is a skier at each location, each skier has initial kinetic energy $ 0 $ . Each skier wants to ski along as many trails as possible. Suppose that the skier is skiing along a trail from location $ i $ to $ j $ . Skiers are not allowed to ski uphill (i.e., if $ h_i < h_j $ ). It costs one unit of kinetic energy to ski along flat ground (i.e., if $ h_i = h_j $ ), and a skier gains one unit of kinetic energy by skiing downhill (i.e., if $ h_i > h_j $ ). For each location, compute the length of the longest sequence of trails that the skier starting at that location can ski along without their kinetic energy ever becoming negative. Skiers are allowed to visit the same location or trail multiple times.输入输出格式
输入格式
The first line contains a single integer $ n $ ( $ 2 \le n \le 2 \cdot 10^5 $ ). The second line contains $ n $ integers $ l_1, l_2, \ldots, l_n $ ( $ 0 \le l_i \le 1 $ ). If $ l_i = 1 $ , location $ i $ is a base lodge; if $ l_i = 0 $ , location $ i $ is not a base lodge. It is guaranteed that there is at least $ 1 $ base lodge. Each of the next $ n-1 $ lines contains two integers $ u, v $ ( $ 1 \leq u, v \leq n $ , $ u \neq v $ ), meaning that there is a trail that connects the locations $ u $ and $ v $ . It is guaranteed that the given trails form a tree.
输出格式
Print $ n $ integers: the $ i $ -th integer is equal to the length of the longest sequence of trails that the skier starting at location $ i $ can ski along without their kinetic energy ever becoming negative.
输入输出样例
输入样例 #1
6
1 1 0 0 0 0
1 3
2 4
3 4
4 5
5 6
输出样例 #1
0 0 1 1 3 5
输入样例 #2
9
0 0 0 0 0 0 1 1 1
1 3
2 3
2 5
3 6
4 5
4 7
5 8
6 9
输出样例 #2
5 3 2 1 1 1 0 0 0
输入样例 #3
14
0 0 0 0 0 0 0 0 0 1 1 1 1 1
1 2
2 5
3 4
4 5
3 6
4 8
5 9
7 8
6 11
7 12
8 13
9 14
10 11
输出样例 #3
8 5 4 3 2 2 1 1 1 0 0 0 0 0
输入样例 #4
20
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0 1
17 3
11 12
6 10
18 19
8 14
16 20
5 3
2 11
7 10
2 15
8 3
3 15
9 16
7 13
16 1
19 2
2 16
6 1
4 17
输出样例 #4
2 2 1 5 3 4 8 1 2 6 4 6 10 0 0 0 3 0 1 0