Painting
Time limit5sMemory limit128 MB
Given an n by n grid painted with k colors, find the minimum number of whole row or column repaints needed to make the entire grid one color, where a line can only be painted color c if it already has at least two tiles of that color, and list all colors achieving that minimum.
- Level
Hard8 of 10
- Topics
- Graph, BFS, Simulation
- Solved
- No attempts yet
Problem
There is a wall made of tiles. Long ago each tile was painted in one of colors. The paint has worn out, so the wall must be repainted, and this time every tile must end up in a single one of the colors.
In one move you may repaint one whole horizontal row or one whole vertical column of tiles into a color of your choice. However, you may paint a row or a column into color only if at least two of its tiles are already color (either from the original painting or from an earlier move).
Every tile must be repainted (covered by at least one move), and in the end all tiles must share the same color. Determine the minimum number of moves needed, and for which target colors that minimum is achieved.
Input
The first line contains the number of test cases. For each test case, the first line contains two integers and with (the number of tiles in a row) and (the number of available colors). Each of the next lines contains integers between and , giving the original color of every tile.
Output
For each test case, print two lines. The first line contains , the minimum number of moves needed to repaint the whole wall into a single color. The second line lists, in increasing order and separated by single spaces, all colors into which the wall can be repainted in exactly moves. If it is impossible to repaint the wall under the rules for any color, print a single line containing just .