Editorial for DMOPC '21 Contest 7 P5 - King's Commands
Submitting an official solution before solving the problem yourself is a bannable offence.
Notice that the size of the largest set of disconnected cities is the same as the number of connected components in the graph. This subtask was created to reward efficient brute force solutions which manually calculate the number of components for each of the interpretations of the king's commands. For example, one efficient method to count the number of components for a fixed set of edges is to first sort the edges in non-decreasing order of their bottom cities. Then, if the largest top city in some prefix of the edges is smaller than the smallest top city in some suffix , we increase the number of components by . Remember to add the number of cities with no incident edges to the final answer; these will henceforth be known as "lone" cities.
This subtask was created to reward inefficient implementations of the correct algorithm presented in subtask 4.
For this subtask, notice that the number of lone cities is constant over all interpretations. So, we only seek to minimize the number of components formed by the edges. For convenience of notation, let's define and . Also, we say an edge is left-oriented if and right-oriented otherwise. The first thing to notice is that if we have a pair of indices where and , then the -th edge is effectively useless since edges and will always be connected regardless of their respective orientations. After removing all useless edges, we may order the edges such that and for all .
At this point, we orient the edges so that all edges with an odd index are left-oriented and all edges with an even index are right-oriented. It is easy to verify that such an arrangement ensures that the two edges are connected if , which is also the only case where it is possible to connect and . Since all pairs of edges that can possibly be connected are indeed connected, this construction produces a valid interpretation for this subtask that minimizes the number of components. The diagram below provides a visual illustration of an arrangement that this algorithm may produce. Left-oriented edges are marked in red, right-oriented edges are marked in blue, and useless edges are marked in black:
Without the constraint that each city number appears at most once, the number of lone cities may now vary based on the orientations of the edges. Let's step back for a minute and make a huge claim inspired by the solutions to the previous subtasks: If we sort the edges in non-decreasing order of their bottom cities and partition the array into as many blocks as possible such that the largest top city up to index is no greater than the smallest top city from onwards for all , then each block of edges can be oriented in a manner such that the entire block is connected and no city has two incident edges (i.e. the number of lone cities is strictly minimized). Note that for any where the largest top city up to index is equal to the smallest top city from onwards, say , then one of the cities at could have two incident edges. However, if we flip the orientation of all edges from block to block we will increase the number of components by and decrease the number of lone cities by , leaving the overall answer unchanged. Repeating this process, we see that the claim really suggests that there is some optimal arrangement where the number of lone cities is globally minimized (equal to ). If this claim holds true then any arrangement of this form is clearly optimal, as it both minimizes the number of components as well as the number of lone cities. Now, let's back our claim up by construction.
First of all, to ensure that no city has two incident edges, edges sharing an endpoint must be arranged so that both cities at that endpoint are connected to an edge. In particular, this means the orientation of one of these edges uniquely determines the other. Since each city number appears at most twice, the only implication structures that may appear are chains and cycles. Let us only focus on chains first. In fact, let's start off by assuming all of the implications in the graph are chains where (simple, increasing chains). For each chain, let its left and right endpoints be and respectively. It is easy to see that different chains will have different and , so the set of 's and 's is once again unique. This should motivate us to return to the construction provided in the previous subtask, except we now work with chains of edges instead of just the edges themselves. One thing that is important to note is that none of the chains are "useless" anymore; chains with and must have opposite orientations, or we risk having a suboptimal arrangement. For example, another diagram is provided below to illustrate a possible arrangement this algorithm produces, this time using different colours to denote different chains (orientations should be self-evident at this point):
Intuitively, the construction works because any intersection of chains is always optimally oriented in opposite orientations. It should be clear that two overlapping chains with the same orientation can never produce a better answer than the same two chains with opposite orientations. To arrive at a more rigorous proof, we may consider the connection point shared between two adjacent edges in the same chain. If no chain crosses through this point then the left and right portions will be disconnected, which is consistent with our claim because the two edges of the chain would reside in different blocks. Otherwise, if there exist some edges that cross over the connection point, they must be from a different chain. By construction, at least one of these chains will have the opposite orientation, thus "stringing" the two edges together and merging them into a single component. This is once again consistent with our previous claim, as it reflects the case where these edges belong to the same block.
Returning back to the task at hand, we have still yet to consider chains with more complex patterns and cycles. Luckily, these do not serve as too much of an extra obstacle. A chain that reverses back on itself can be simplified and viewed as just a single edge, as it accepts a superset of the connections that the single edge accepts:
In a similar fashion, cycles are even nicer to work with; they simplify into two chains sharing the same endpoints that are left-oriented and right-oriented at the same time, which means that we can simply select their orientation arbitrarily! Any complex chains and cycles can be simplified and treated as simple chains, so our solution extends naturally to the general case.
- The problem was initially written as merely subtask 3, serving as a mid-level fourth problem in an average DMOPC problemset. However, when attempting to simplify the input from total cities to total cities, I ran into issues with cases where some cities are mentioned more than once. In fact, the first case which I struggled with for the longest is included as the fifth case of the first batch, containing edges . After a few days of struggling with the generalized version, I eventually found the solution presented above after imposing the extra constraint that no city number appears more than twice. What seemed like just a tiny tweak in the constraints surprisingly managed to elevate the problem into becoming one of the hardest DMOPC problems in recent history.
- The fully generalized version (that is, without the constraint on the frequency of each city) remains unsolved in polynomial time, and is still very much an open-ended problem. If you have made any progress on it, I would love to discuss the problem with you in the comments section below.