Editorial for Checkerboard Cut
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
For subtask 1, note that there are
Time complexity:
Subtask 2
For subtask 2, we can still afford to brute-force all
Time complexity:
Subtasks 3, 4, 5
Instead of assigning colours after we made the cuts, let's consider the problem from a different perspective, where we add in the cuts one by one in some order and change the colouring after each time we add a cut. Starting from a grid where we keep all of the cells, we can notice that adding a cut corresponds to flipping the state of all cells in a half-plane from "kept" to "not kept". From this, we can notice that the set of all colourings obtainable through such flippings is equivalent to the set of all colourings obtainable through repeatedly flipping the state of all cells in some row or column. This version is much easier to work with.
Consider assigning a boolean variable
We can now obtain polynomial-time algorithms for the remaining subtasks:
- For subtask 3, we can consider all pairs
for some fixed and run a max-flow algorithm like Edmonds-Karp to find the minimum weighted -cut, and take the minimum of all of these to find the global min-cut. Edmonds-Karp takes time on a graph with nodes and edges, which is in our case, and since we need to run it times we get a time complexity of overall. In practice, flow algorithms like Edmonds-Karp run much faster in practice and this passes subtask 3. - For subtask 4, we can speed up the above solution by using a faster flow algorithm such as Dinic's or Push-Relabel. Dinic's algorithm gives a time complexity of
and Push-Relabel gives a time complexity of , both of which can pass subtask 4 if well-implemented since they run fast in practice. Alternatively, a slower algorithm designed specifically for the global min-cut problem such as the randomized Karger's algorithm can be used. - For subtask 5, we can use the Stoer-Wagner algorithm, which runs in
time.
Comments