Editorial for DMOPC '21 Contest 7 P5 - King's Commands
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
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
Time Complexity:
Subtask 2
This subtask was created to reward inefficient implementations of the correct algorithm presented in subtask 4.
Time Complexity:
Subtask 3
For this subtask, notice that the number of lone cities is constant over 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
Time Complexity:
Subtask 4
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
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
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.
Time Complexity:
Remarks:
- 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.
Comments