Editorial for Wesley's Anger Contest 5 Problem 6 - Squirrel Cities 2
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
For
For
Time Complexity:
Subtask 2
Since the graph is a cactus, each edge is only part of one cycle. It can be seen that the choice of whether or not to choose a local road does not affect the choices of whether or not to choose a different local road. In addition, we can see that each local road will decrease the sum of lengths by a fixed amount. Thus, we can use a greedy algorithm to pick the
Time Complexity:
Subtask 3
We can see that the shortest path between any two cities in the highway network plus one local road either passes through the local road or it does not. If it does not pass through the local road, then it must use only highways. We can precompute the length of the shortest path using only highways for each pair of cities by performing a breadth first search from each city. We can now get the distance between cities
Time Complexity:
Subtask 4
The greedy algorithm in subtask 2 can be similarly applied to subtask 3.
Time Complexity:
Subtask 5
Instead of performing
Time Complexity:
Subtask 6
The greedy algorithm in subtask 2 can be similarly applied to subtask 6.
Time Complexity:
Subtask 7
If we arbitrarily root the tree of highways, we can observe that any path between two cities will first go up the tree, reaches the lowest common ancestor, and then go back down the tree. We can also see that for each cycle in the cactus graph, the cycle has a unique vertex that is closest to the root of the tree. We can call this vertex the root of the cycle. If we consider all vertices in a cycle, the local road will affect all travel plans passing through that vertex by a fixed amount. For each local road, we can compute this amount for each vertex in the cycle, and multiply it by the number of paths going through that vertex and up to the root of the cycle (which can be done using difference arrays). This amount will be added to the decrease in the sum of lengths for that local road. For cases where a path does not go through the root of the cycle (which means its lowest common ancestor is a non root vertex in the cycle), we can handle them each separately since this only happens at most once for each travel plan. We can see that the path in this case passes through some vertices not in the cycle, followed by some vertices in the cycle, and then finally some more vertices not in the cycle. The decrease in this case is equal to the minimum distance in the cycle between the first cycle vertex in the path, and the last cycle vertex in the path subtracted from the distance between those two vertices in the original tree. We can then choose the
Time Complexity:
Comments