CF1654G.Snowy Mountain

省选/NOI-

通过率:0%

时间限制:5.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

There are nn locations on a snowy mountain range (numbered from 11 to nn), connected by n−1n-1 trails in the shape of a tree. Each trail has length 11. Some of the locations are base lodges. The height hih_i of each location is equal to the distance to the nearest base lodge (a base lodge has height 00).

There is a skier at each location, each skier has initial kinetic energy 00. Each skier wants to ski along as many trails as possible. Suppose that the skier is skiing along a trail from location ii to jj. Skiers are not allowed to ski uphill (i.e., if hi<hjh_i \lt h_j). It costs one unit of kinetic energy to ski along flat ground (i.e., if hi=hjh_i = h_j), and a skier gains one unit of kinetic energy by skiing downhill (i.e., if hi>hjh_i \gt 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.

一座被积雪覆盖的山脉上有 nn 个地点(编号为 11 到 nn),这些地点通过 n−1n-1 条小径相连,构成一棵树。每条小径的长度均为 11。其中部分地点是山脚旅馆。每个地点 ii 的高度 hih_i 定义为该地点到最近的山脚旅馆的距离(山脚旅馆自身的高度为 00)。

每个地点上都有一名滑雪者,且初始动能均为 00。每位滑雪者希望尽可能多地滑行经过小径。假设某滑雪者正沿一条从小径一端地点 ii 滑向另一端地点 jj。滑雪者不允许向上坡滑行(即若 hi<hjh_i < h_j,则禁止滑行)。在平地上滑行(即 hi=hjh_i = h_j)消耗 11 单位动能;而向下滑行(即 hi>hjh_i > h_j)则获得 11 单位动能。对每个地点,请计算:从该地点出发的滑雪者,在其动能始终不为负的前提下,所能滑行的最长小径序列的长度(即经过的小径条数)。滑雪者可以多次访问同一地点或同一条小径。

输入格式

The first line contains a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5).

The second line contains nn integers l1,l2,…,lnl_1, l_2, \ldots, l_n (0≤li≤10 \le l_i \le 1). If li=1l_i = 1, location ii is a base lodge; if li=0l_i = 0, location ii is not a base lodge. It is guaranteed that there is at least 11 base lodge.

Each of the next n−1n-1 lines contains two integers u,vu, v (1≤u,v≤n1 \leq u, v \leq n, u≠vu \neq v), meaning that there is a trail that connects the locations uu and vv. It is guaranteed that the given trails form a tree.

第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)。

第二行包含 nn 个整数 l1,l2,…,lnl_1, l_2, \ldots, l_n(0≤li≤10 \le l_i \le 1)。若 li=1l_i = 1,则位置 ii 是一个基地小屋;若 li=0l_i = 0,则位置 ii 不是基地小屋。保证至少存在 11 个基地小屋。

接下来的 n−1n-1 行中,每行包含两个整数 u,vu, v(1≤u,v≤n1 \leq u, v \leq n,且 u≠vu \neq v),表示存在一条连接位置 uu 和 vv 的小径。保证所给的小径构成一棵树。

输出格式

Print nn integers: the ii-th integer is equal to the length of the longest sequence of trails that the skier starting at location ii can ski along without their kinetic energy ever becoming negative.

输出 nn 个整数:第 ii 个整数等于滑雪者从位置 ii 出发,在动能始终不为负的前提下,所能滑行的最长路径序列的长度。

输入输出样例

  • 输入#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]h = [0, 0, 1, 1, 2, 3]. The skier starting from 66 can ski along at most 55 trails, in the path 6→5→4→3→4→26 \rightarrow 5 \rightarrow 4 \rightarrow 3 \rightarrow 4 \rightarrow 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 66, the kinetic energy is 00;
  • at the location 55, the kinetic energy increases by 11 (because h5<h6h_5 \lt h_6), so it becomes 11;
  • at the location 44, the kinetic energy increases by 11 (because h4<h5h_4 \lt h_5), so it becomes 22;
  • at the location 33, the kinetic energy decreases by 11 (because h3=h4h_3 = h_4), so it becomes 11;
  • at the location 44, the kinetic energy decreases by 11 (because h4=h3h_4 = h_3), so it becomes 00;
  • at the location 22, the kinetic energy increases by 11 (because h2<h4h_2 \lt h_4), so it becomes 11.

There isn't any sequence of trails of length greater than 55 such that the kinetic energy is always non-negative.

Moreover,

  • the optimal path for the skier starting from 11 is 11 (no trails);
  • the optimal path for the skier starting from 22 is 22 (no trails);
  • the optimal path for the skier starting from 33 is 3→13 \rightarrow 1;
  • the optimal path for the skier starting from 44 is 4→24 \rightarrow 2;
  • the optimal path for the skier starting from 55 is 5→4→3→15 \rightarrow 4 \rightarrow 3 \rightarrow 1.

In the second test, h=[3,2,2,1,1,1,0,0,0]h = [3, 2, 2, 1, 1, 1, 0, 0, 0]. The skier starting from 11 can ski along at most 55 trails, in the path 1→3→2→5→4→71 \rightarrow 3 \rightarrow 2 \rightarrow 5 \rightarrow 4 \rightarrow 7.

  • at the location 11, the kinetic energy is 00;
  • at the location 33, the kinetic energy increases by 11 (because h3<h1h_3 \lt h_1), so it becomes 11;
  • at the location 22, the kinetic energy decreases by 11 (because h2=h3h_2 = h_3), so it becomes 00;
  • at the location 55, the kinetic energy increases by 11 (because h5<h2h_5 \lt h_2), so it becomes 11;
  • at the location 44, the kinetic energy decreases by 11 (because h4=h5h_4 = h_5), so it becomes 00;
  • at the location 77, the kinetic energy increases by 11 (because h7<h4h_7 \lt h_4), so it becomes 11.

There isn't any sequence of trails of length greater than 55 such that the kinetic energy is always non-negative.

In the third test, for the skier starting from vertex 11, the optimal path is 1→2→5→4→3→6→11→10→111 \rightarrow 2 \rightarrow 5 \rightarrow 4 \rightarrow 3 \rightarrow 6 \rightarrow 11 \rightarrow 10 \rightarrow 11.

Here are pictures of the first, second, and third test, with the base lodges shown in red:

在第一个测试中,h=[0,0,1,1,2,3]h = [0, 0, 1, 1, 2, 3]。从位置 66 出发的滑雪者最多可沿 55 条滑道滑行,路径为 6→5→4→3→4→26 \rightarrow 5 \rightarrow 4 \rightarrow 3 \rightarrow 4 \rightarrow 2(注意:滑雪者可多次沿同一条滑道滑行,也可多次访问同一位置):

  • 在位置 66,动能为 00;
  • 在位置 55,动能增加 11(因为 h5<h6h_5 \lt h_6),变为 11;
  • 在位置 44,动能增加 11(因为 h4<h5h_4 \lt h_5),变为 22;
  • 在位置 33,动能减少 11(因为 h3=h4h_3 = h_4),变为 11;
  • 在位置 44,动能减少 11(因为 h4=h3h_4 = h_3),变为 00;
  • 在位置 22,动能增加 11(因为 h2<h4h_2 \lt h_4),变为 11。

不存在长度大于 55 的滑道序列,使得全程动能始终非负。

此外,

  • 从位置 11 出发的滑雪者的最优路径为 11(不经过任何滑道);
  • 从位置 22 出发的滑雪者的最优路径为 22(不经过任何滑道);
  • 从位置 33 出发的滑雪者的最优路径为 3→13 \rightarrow 1;
  • 从位置 44 出发的滑雪者的最优路径为 4→24 \rightarrow 2;
  • 从位置 55 出发的滑雪者的最优路径为 5→4→3→15 \rightarrow 4 \rightarrow 3 \rightarrow 1。

在第二个测试中,h=[3,2,2,1,1,1,0,0,0]h = [3, 2, 2, 1, 1, 1, 0, 0, 0]。从位置 11 出发的滑雪者最多可沿 55 条滑道滑行,路径为 1→3→2→5→4→71 \rightarrow 3 \rightarrow 2 \rightarrow 5 \rightarrow 4 \rightarrow 7。

  • 在位置 11,动能为 00;
  • 在位置 33,动能增加 11(因为 h3<h1h_3 \lt h_1),变为 11;
  • 在位置 22,动能减少 11(因为 h2=h3h_2 = h_3),变为 00;
  • 在位置 55,动能增加 11(因为 h5<h2h_5 \lt h_2),变为 11;
  • 在位置 44,动能减少 11(因为 h4=h5h_4 = h_5),变为 00;
  • 在位置 77,动能增加 11(因为 h7<h4h_7 \lt h_4),变为 11。

不存在长度大于 55 的滑道序列,使得全程动能始终非负。

在第三个测试中,从顶点 11 出发的滑雪者的最优路径为 1→2→5→4→3→6→11→10→111 \rightarrow 2 \rightarrow 5 \rightarrow 4 \rightarrow 3 \rightarrow 6 \rightarrow 11 \rightarrow 10 \rightarrow 11。

以下是第一、第二和第三个测试的示意图,其中基础小屋以红色标出:

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

首页