Given a grid where some cells are unlit (unions of rectangles), find the fewest cell states to flip so that a set of alternating row-column dances can cover all lit cells, each dance starting and ending on the same cell with different first and last feet.
Hard9GraphGreedyMathImplementationNo attempts yetTime limit5sMemory limit512 MBDaryl owns a disco club named the Disco Dance Den. Its dance floor is an m×n grid of cells. Every cell is lit at first. When the music starts, some of the cells go unlit.
A disco dance is a sequence of at least two steps taken while the music plays. The last step of a disco dance lands on the cell the dancer started from.
A dancer steps only on lit cells. The moment he steps on a lit cell, that cell goes unlit. A dancer also obeys these four conditions:
Daryl's Disco Dance Den is dark when every cell of the dance floor is unlit.
Daryl offers a dare to a group of dancers:
As an example, suppose the setup is this:

Two dancers can then meet the dare by starting on the cells marked on the left side of the figure below.

Once both dancers finish, the Disco Dance Den goes dark because every lit cell has been stepped on, and both dancers obeyed all four conditions.
Can the dancers meet the dare? If they cannot, what is the smallest number of cells whose state must be flipped (from lit to unlit, or from unlit to lit) right after the music starts so that the dare becomes possible?
The first line contains one integer T, the number of test cases. The test cases follow.
The first line of each test case contains three space separated integers m, n, and k: the number of rows of the grid, the number of columns of the grid, and the number of groups of cells that go unlit once the music starts.
Each of the next k lines contains four space separated integers i0, j0, i1, j1. For every i and j with i0≤i≤i1 and j0≤j≤j1, the cell in row i and column j goes unlit when the music starts. The groups of unlit cells may overlap.
Constraints
For each test case print one line with a single integer N, the smallest number of cells whose state must be flipped so that the dancers can complete Daryl's disco dare. Print 0 when they can already complete it.
In the first test case of the first example the groups of unlit cells overlap. Flipping one cell, the cell in row 4 and column 2, turns the floor into the grid shown in the figure above, so the answer is 1.