Editorial for A Farewell to Holds
Submitting an official solution before solving the problem yourself is a bannable offence.
Model every row and every column as a vertex. Cell becomes an edge between the row vertex
and the column vertex
. This edge has color
and value
.
So the task is equivalent to the following graph problem: choose a matching of edges to delete, so that after deleting those edges the remaining edge coloring is proper. That means no two edges with the same color are incident to the same vertex. Among all valid choices, minimize the maximum value of a deleted edge.
Let be the number of edges and
be the number of vertices.
Checking a Fixed Answer
Binary search the answer. Suppose we want to check whether cost at most is possible.
For every edge , create a Boolean variable
. Let
mean that edge
is deleted, and
mean that it remains.
If , then edge
cannot be deleted, so add the 2-SAT clause:
Now consider the color constraint at one vertex. For each color, look at the incident edges with that color.
If there are at least three such edges, the answer is impossible. At least two of them would need to be deleted, but two deleted edges incident to the same vertex cannot both belong to a matching.
If there are exactly two such edges, say and
, at least one of them must be deleted:
It remains to enforce that the deleted edges form a matching. For every vertex , let its incident edges be:
We need at most one of to be true. Adding all pairwise clauses would be too slow, so use the standard linear 2-SAT encoding with auxiliary variables
, where
means that at least one of
has been deleted.
Add these clauses:
These clauses ensure that once one incident edge of is deleted, no later incident edge of
can also be deleted.
All constraints are now 2-SAT clauses. Build the implication graph and find strongly connected components. The threshold is feasible if and only if no variable and its negation are in the same strongly connected component.
Complexity
For a fixed , the number of variables and clauses is
, because the matching constraints are encoded linearly over all incidences.
The 2-SAT check using SCC also takes time. Binary searching the answer gives total complexity:
If even is infeasible, print
No. Otherwise, print Yes and the smallest feasible value of .
Comments