Editorial for Yet Another Contest 9 P5 - Road Redistribution
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
We can model the village as a connected undirected graph with nodes and edges. Due to these properties, the graph must contain exactly one cycle. Each move corresponds to deleting an edge in the cycle, leaving a tree behind; then, an edge is added.
There are many solutions with a suboptimal number of moves. For example, you can repeatedly delete any cycle edge (choosing an edge which is not in if one exists, and choosing any edge otherwise), and then add an edge in which is not in the current graph.
Let's now focus on an optimal construction. Let be the number of edges in which are not in . Note that this is equal to the number of edges in which are not in . Each move can reduce by at most , and will be decreased by if and only if we remove an edge that is not in , and add an edge that is in . It follows that we require at least moves. In addition, note that if all of 's cycle edges are in , the very first move cannot decrease , and so we can tighten the lower-bound to moves. We will now show this lower bound is always attainable.
It turns out we can make a small modification to the construction described above; the only difference is that when adding an edge in which is not in the current graph, we should prioritize edges which are not part of 's cycle. Why? Each move will decrease by if there is a cycle edge in the current graph which is not in , and will keep the same otherwise; that is, it is preferable for the current graph's cycle to not be equal to 's cycle, so that such a cycle edge exists. By prioritizing edges which are not part of 's cycle, we will make sure that the resulting graph does not have the same cycle as 's cycle until the very last move.
Alternative Solution
We will ignore the case where and are already equal. Then, any construction has the following structure:
- First, delete a cycle edge, leaving a tree.
- Then, perform zero or more operations in which we add an edge to the tree, and then delete a cycle edge, leaving a tree.
- Finally, add an edge, making the current graph equal to .
Here, we use the term operation (distinct from move) to denote adding and then removing an edge as described above. This is useful as we can now work with transformations from one tree to another, rather than road configurations with cycles.
If there is any cycle edge in which is not in , then we can choose this edge to be the very first deleted edge. Otherwise, we can delete any cycle edge in arbitrarily. We will then make some number of operations, until all edges in our current graph are in - at this point, simply add the final edge which is in but not in the current graph, and we will be done.
All that's left is to ensure that each operation adds an edge which is in (but not in the current graph), and then deletes a cycle edge which is not in . First, pick any edge that is in the current graph but not in . Consider removing ; this would leave a graph with two connected components. As is connected, there must be an edge that is in but not in the current graph which spans these two components. Then, we can choose our operation to add edge and then remove edge .
Comments