Snowy Mountain
Contest: Технокубок 2022 - Финал
Difficulty rating: 2900
Tags: data structures, dfs and similar, graphs, greedy, shortest paths, trees

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.

Input format:
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.

Output format:
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.

Constraints:
Time limit: 5.0 s
Memory limit: 1024.0 MB

Note:
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 \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 $$$6$$$, the kinetic energy is $$$0$$$;
- at the location $$$5$$$, the kinetic energy increases by $$$1$$$ (because $$$h_5 < h_6$$$), so it becomes $$$1$$$;
- at the location $$$4$$$, the kinetic energy increases by $$$1$$$ (because $$$h_4 < h_5$$$), so it becomes $$$2$$$;
- at the location $$$3$$$, the kinetic energy decreases by $$$1$$$ (because $$$h_3 = h_4$$$), so it becomes $$$1$$$;
- at the location $$$4$$$, the kinetic energy decreases by $$$1$$$ (because $$$h_4 = h_3$$$), so it becomes $$$0$$$;
- at the location $$$2$$$, the kinetic energy increases by $$$1$$$ (because $$$h_2 < h_4$$$), 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 \rightarrow 1$$$;
- the optimal path for the skier starting from $$$4$$$ is $$$4 \rightarrow 2$$$;
- the optimal path for the skier starting from $$$5$$$ is $$$5 \rightarrow 4 \rightarrow 3 \rightarrow 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 \rightarrow 3 \rightarrow 2 \rightarrow 5 \rightarrow 4 \rightarrow 7$$$.

- at the location $$$1$$$, the kinetic energy is $$$0$$$;
- at the location $$$3$$$, the kinetic energy increases by $$$1$$$ (because $$$h_3 < h_1$$$), so it becomes $$$1$$$;
- at the location $$$2$$$, the kinetic energy decreases by $$$1$$$ (because $$$h_2 = h_3$$$), so it becomes $$$0$$$;
- at the location $$$5$$$, the kinetic energy increases by $$$1$$$ (because $$$h_5 < h_2$$$), so it becomes $$$1$$$;
- at the location $$$4$$$, the kinetic energy decreases by $$$1$$$ (because $$$h_4 = h_5$$$), so it becomes $$$0$$$;
- at the location $$$7$$$, the kinetic energy increases by $$$1$$$ (because $$$h_7 < h_4$$$), 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 \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:

Example input:
6
1 1 0 0 0 0
1 3
2 4
3 4
4 5
5 6

Example output:
0 0 1 1 3 5

Example input:
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

Example output:
5 3 2 1 1 1 0 0 0

Editorial:
Hint 1
Let's say that a vertex is "flippable" if it has at least one neighbor of the same height. The optimal strategy for a skier is to ski to a flippable vertex with lowest height, flip back and forth between that vertex and its neighbor until all energy is used, and then ski to a base lodge. Hint 2
Assuming the skier starts at vertex v and chooses vertex u as the flippable vertex to flip back and forth at, this skier will "waste" a total of hu units of kinetic energy for a total path length of 2hv−hu. Note that if no such u exists, then the maximum amount of hv will be wasted as the skier goes straight to a base lodge. Let wv be this "wasted" amount. Instead of computing the maximum path length for the skier starting at vertex v, compute wv. The answer for that vertex is 2hv−wv
. Hint 3
Try to solve this seemingly unrelated problem: Let S be the set of flippable vertices. What is the largest possible value of sum(v) in Shv
? Hint 4
The largest possible value of sum(v) in Shv is O(n). Therefore, there are at most O(sqrt(n)) distinct values of wv across all vertices. Solve for each value of wv
separately. Solution
Read the hints. The rest is just implementation.
For each set of flippable vertices of the same height, we can calculate the set of starting vertices which are able to reach at least one vertex in that flippable set. To do this, split the graph up into layers of equal height. Let cv be the minimum required energy to reach a vertex in the flippable set. cv can be computed via shortests paths, where edges in the same layer have weight 1 and edges from layer i to i+1 have weight −1. We can use bfs to relax the costs of vertices in a single layer, and then easily transition to the next layer. We do this for O(sqrt(n)) different starting heights, so the total complexity is O(nsqrt(n)).