Editorial for An Animal Contest 2 P6 - Koala Transport System


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Authors: Riolku, ThingExplainer

Subtask 1

First, we run a DFS from node 1 to build a retrace array. Let retrace[i] be the next node on the path from i to 1, and retrace[1] will store a dummy node, such as 0.

Let dist[i][j] store the minimal distance from any node on the path 1 \dots i to node j. We can fill the retrace array by running a multi-node BFS for every node i. To do this, simply use the retrace array and push the nodes from the path i \dots 1 into the queue, and perform BFS normally.

Now, we can process the queries by setting each node as v and summing the distance for all x_i by using the dist array, and taking the minimum value after setting all N nodes.

Time Complexity: \mathcal{O}(N^2 + QN)

Subtask 2

To solve this subtask, we only need to run 3 DFS for the single query.

We initialize an array subtree to store the number of feeding grounds in the subtree of node i. We can fill up this array by running a DFS from node 1.

We now make the observation that the best teleportation path sets v as a leaf node. Why? Let's say that the best teleportation path does not set v as a leaf node. We can make this teleportation path better by setting one of the children of v as v, lets call this child w. This allows any feeding grounds in the subtree of w closer access to a teleporter and decreases or maintains the total distance travelled from all feeding grounds to node 1.

Let totaldist represent the total distance travelled from all feeding grounds to node 1 without the aid of any teleporters and m represent some total distance travelled from all feeding grounds to node 1 that can be achieved with the aid of teleporters. We note that m is just totaldist subtracted by some distance d.

Therefore, we will attempt to maximize d.

We now run a DFS with an additional currsum variable which stores the current sum of the distance that can be subtracted if we have teleporters from 1 \dots u, where u is the current node we are running DFS on. For all nodes v in the subtree of u, we set the node v as a teleporter by simply increasing currsum by the number of feeding grounds in the subtree of v. This is because all feeding grounds in the subtree of v now have 1 less distance to travel to reach a teleporter. When we reach a leaf node, we simply set maxsubtract to \max(currsum, maxsubtract).

We now run our last DFS which will get the total distance for all feeding grounds to node 1 if there were no teleporters installed. To do this, keep a dist variable when running DFS to store the distance from the current node to node 1. Now, for all feeding grounds x which we encounter during our DFS, we can simply add our dist variable to totaldist.

Our answer is simply totaldist - maxsubtract.

Note that the last 2 DFS can be merged for a faster runtime.

Time Complexity: \mathcal{O}(N)

Subtask 3

Let's consider a set of candidate locations c.

Suppose we consider the root as our candidate. Our answer would be the sum of the depths of the feeding locations.

Now suppose we have a different candidate location. How much do we save by moving the node v from the root to such a location? Every time we "move in the direction of a feeding location", we save 1 unit of time per feeding location. A good way to calculate this is for each feeding location, add 1 to every node on the path from it to the root. In order to determine the amount of time we save, we can simply query the path sum of our node c_i.

The final observation is similar to one noted in the previous subtask: leaves are always optimal. However, we see that choosing a feeding location is always at least as optimal as a leaf, since anything lower than the lowest feeding location is not more optimal.

Hence, we consider only the feeding locations as candidates. To implement path update and path query efficiently, we can use HLD with two BITs, although other solutions exist.

Time Complexity: \mathcal{O}(Q \log^2 N)


Comments

There are no comments at the moment.