## Editorial for Checkerboard Cut

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

Author:

#### Subtask 1

For subtask 1, note that there are ways of choosing which horizontal cuts to make, and ways of choosing which vertical cuts to make. We can check all ways of cutting in time each and output the maximum answer.

**Time complexity:**

#### Subtask 2

For subtask 2, we can still afford to brute-force all ways of choosing which horizontal cuts to make, but we need a better method to find the maximum possible answer given some arrangement of horizontal cuts. We can actually do this greedily: There are two options for which cells in some column we get to keep, and we should always take the option with a higher value. However, we need to be careful with the case when there are no horizontal cuts to make sure we don't consider the possibility of making no cuts. This allows for checking some arrangement of horizontal cuts in time each.

**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 to each row and a variable to each column, representing whether we should flip this row/column. We can keep a cell with value if and only if and are the same. Conversely, we don't get to keep the cell if and only if and are different. Now consider a complete weighted bipartite graph where the nodes on the left side correspond to rows and the nodes on the right side correspond to columns, and the weights are equal to the corresponding . We can see that the problem reduces to a global min-cut problem: indeed, the set of or that are correspond to one side of a cut, the set of or that are correspond to the other side of the cut, and the cells we don't get to keep correspond to edges that need to be cut to separate the sets.

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