## Editorial for Wesley's Anger Contest 5 Problem 6 - Squirrel Cities 2

**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.**

Author:

##### Subtask 1

For , we can run a breadth first search for each squirrel to determine the length of its travel plan. The answer is the sum of the lengths.

For , we can construct a new graph for each of the local roads combined with the highways. We can then run a breadth first search for each squirrel on the new graph to determine the minimum sum of lengths using one local road.

**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 local roads with the largest decreases. We can use an algorithm similar to subtask and store the difference between the sum of lengths in the new graph and the sum of lengths in the tree. The sum of the largest elements can then be found using a linear time selection algorithm (or with any standard sorting algorithm).

**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 and in constant time. In the case where the shortest path passes through the local road, it must first pass through some (possible ) highways, pass through the local road, and then through some more (possibly ) highways. Thus, the shortest path between cities and using the local road (between cities and ) is where is the distance between cities and using only highways.

**Time Complexity:**

##### Subtask 4

The greedy algorithm in subtask 2 can be similarly applied to subtask 3.

**Time Complexity:**

##### Subtask 5

Instead of performing breadth first searches to precompute for all pairs of cities, we can instead use the fact that the distance between two vertices in a tree rooted at an arbitrary vertex is equal to . We can query for the lowest common ancestor () in time with or precomputation using any data structure that supports range minimum queries over Euler tour indices.

**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 best local roads similar to subtask 2.

**Time Complexity:**

## Comments