CF1654G.Snowy Mountain
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 hi 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 hi<hj ). It costs one unit of kinetic energy to ski along flat ground (i.e., if hi=hj ), and a skier gains one unit of kinetic energy by skiing downhill (i.e., if hi>hj ). 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≤n≤2⋅105 ).
The second line contains n integers l1,l2,…,ln ( 0≤li≤1 ). If li=1 , location i is a base lodge; if li=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≤u,v≤n , u=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
说明/提示
In the first test, h=[0,0,1,1,2,3] . The skier starting from 6 can ski along at most 5 trails, in the path 6→5→4→3→4→2 (notice that a skier can ski multiple times along the same trail and can visit more than once the same location):
- at the location 6 , the kinetic energy is 0 ;
- at the location 5 , the kinetic energy increases by 1 (because h5<h6 ), so it becomes 1 ;
- at the location 4 , the kinetic energy increases by 1 (because h4<h5 ), so it becomes 2 ;
- at the location 3 , the kinetic energy decreases by 1 (because h3=h4 ), so it becomes 1 ;
- at the location 4 , the kinetic energy decreases by 1 (because h4=h3 ), so it becomes 0 ;
- at the location 2 , the kinetic energy increases by 1 (because h2<h4 ), so it becomes 1 .
There isn't any sequence of trails of length greater than 5 such that the kinetic energy is always non-negative.
Moreover,
- the optimal path for the skier starting from 1 is 1 (no trails);
- the optimal path for the skier starting from 2 is 2 (no trails);
- the optimal path for the skier starting from 3 is 3→1 ;
- the optimal path for the skier starting from 4 is 4→2 ;
- the optimal path for the skier starting from 5 is 5→4→3→1 .
In the second test, h=[3,2,2,1,1,1,0,0,0] . The skier starting from 1 can ski along at most 5 trails, in the path 1→3→2→5→4→7 .
- at the location 1 , the kinetic energy is 0 ;
- at the location 3 , the kinetic energy increases by 1 (because h3<h1 ), so it becomes 1 ;
- at the location 2 , the kinetic energy decreases by 1 (because h2=h3 ), so it becomes 0 ;
- at the location 5 , the kinetic energy increases by 1 (because h5<h2 ), so it becomes 1 ;
- at the location 4 , the kinetic energy decreases by 1 (because h4=h5 ), so it becomes 0 ;
- at the location 7 , the kinetic energy increases by 1 (because h7<h4 ), so it becomes 1 .
There isn't any sequence of trails of length greater than 5 such that the kinetic energy is always non-negative.
In the third test, for the skier starting from vertex 1 , the optimal path is 1→2→5→4→3→6→11→10→11 .
Here are pictures of the first, second, and third test, with the base lodges shown in red: