Gridev's Protocol
Time limit2sMemory limit512 MB
Given M error cells on an N by N grid, find the fewest row or column clears (each allowed when the line has at most K errors) that remove every error, or report -1.
Problem
Seongjun is a robot that solves any problem with ease. Geungsu, jealous of him, damaged his circuit board while he was asleep.
Fortunately, Seongjun has a self-repair protocol, and he plans to use it to fix himself. Seongjun's circuit board is an grid, and currently there are errors at coordinates . Seongjun can repeat the following two kinds of actions.
- For every row with at most errors, remove all errors in that row. The state of a row with more than errors does not change.
- For every column with at most errors, remove all errors in that column. The state of a column with more than errors does not change.
Seongjun wants to repair himself quickly and get revenge on Geungsu. Find the way to remove all errors with the fewest actions.
Input
The first line contains the size of the board , the number of errors , and the maximum number of errors that can be repaired in one row or column, separated by spaces. ()
From the second line to the -th line, the positions of the errors are given, separated by spaces. ()
No error position is given more than once.
Output
Print the minimum number of actions needed to remove all errors. If there is no way to remove all errors, print -1.