Editorial for A Farewell to Holds


Model every row and every column as a vertex. Cell (i,j)(i,j) becomes an edge between the row vertex RiR_i and the column vertex CjC_j. This edge has color ci,jc_{i,j} and value ai,ja_{i,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 EE be the number of edges and VV be the number of vertices.

Checking a Fixed Answer

Binary search the answer. Suppose we want to check whether cost at most ww is possible.

For every edge ee, create a Boolean variable xex_e. Let xe=1x_e = 1 mean that edge ee is deleted, and xe=0x_e = 0 mean that it remains.

If ae>wa_e > w, then edge ee cannot be deleted, so add the 2-SAT clause:

¬xe\lnot x_e

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 gg and hh, at least one of them must be deleted:

xg∨xhx_g \lor x_h

It remains to enforce that the deleted edges form a matching. For every vertex uu, let its incident edges be:

e1,e2,…,eke_1, e_2, \ldots, e_k

We need at most one of xe1,xe2,…,xekx_{e_1}, x_{e_2}, \ldots, x_{e_k} 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−1y_1, y_2, \ldots, y_{k-1}, where yiy_i means that at least one of e1,e2,…,eie_1, e_2, \ldots, e_i has been deleted.

Add these clauses:

¬xei∨yi(1≤i<k)\lnot x_{e_i} \lor y_i \qquad (1 \leq i < k) ¬yi−1∨yi(2≤i<k)\lnot y_{i-1} \lor y_i \qquad (2 \leq i < k) ¬yi∨¬xei+1(1≤i<k)\lnot y_i \lor \lnot x_{e_{i+1}} \qquad (1 \leq i < k)

These clauses ensure that once one incident edge of uu is deleted, no later incident edge of uu can also be deleted.

All constraints are now 2-SAT clauses. Build the implication graph and find strongly connected components. The threshold ww is feasible if and only if no variable and its negation are in the same strongly connected component.

Complexity

For a fixed ww, the number of variables and clauses is O(E+V)O(E+V), because the matching constraints are encoded linearly over all incidences.

The 2-SAT check using SCC also takes O(E+V)O(E+V) time. Binary searching the answer gives total complexity:

O(log⁡(109)⋅(E+V)).O(\log(10^9) \cdot (E+V)).

If even w=109w = 10^9 is infeasible, print No. Otherwise, print Yes and the smallest feasible value of ww.

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.