Editorial for A Farewell to Holds
Model every row and every column as a vertex. Cell (i,j) becomes an edge between the row vertex Ri and the column vertex Cj. This edge has color ci,j and value ai,j.
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 E be the number of edges and V be the number of vertices.
Checking a Fixed Answer
Binary search the answer. Suppose we want to check whether cost at most w is possible.
For every edge e, create a Boolean variable xe. Let xe=1 mean that edge e is deleted, and xe=0 mean that it remains.
If ae>w, then edge e cannot be deleted, so add the 2-SAT clause:
¬xeNow 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 g and h, at least one of them must be deleted:
xg∨xhIt remains to enforce that the deleted edges form a matching. For every vertex u, let its incident edges be:
e1,e2,…,ekWe need at most one of xe1,xe2,…,xek to be true. Adding all pairwise clauses would be too slow, so use the standard linear 2-SAT encoding with auxiliary variables y1,y2,…,yk−1, where yi means that at least one of e1,e2,…,ei has been deleted.
Add these clauses:
¬xei∨yi(1≤i<k) ¬yi−1∨yi(2≤i<k) ¬yi∨¬xei+1(1≤i<k)These clauses ensure that once one incident edge of u is deleted, no later incident edge of u can also be deleted.
All constraints are now 2-SAT clauses. Build the implication graph and find strongly connected components. The threshold w is feasible if and only if no variable and its negation are in the same strongly connected component.
Complexity
For a fixed w, the number of variables and clauses is O(E+V), because the matching constraints are encoded linearly over all incidences.
The 2-SAT check using SCC also takes O(E+V) time. Binary searching the answer gives total complexity:
O(log(109)⋅(E+V)).If even w=109 is infeasible, print No. Otherwise, print Yes and the smallest feasible value of w.
Comments0
No comments yet
Be the first to comment.
New comment
Log in to join the discussion.