Alice and rooks
Practice
4 (2 votes)
Medium
Problem
82% Success 182 Attempts 30 Points 4s Time Limit 256MB Memory 1024 KB Max Code

Alice has a chess board with n rows and m columns. There are k obstacles on the board placed exactly in cells. Two different rooks form a bad pair if they stay in one row or in one column and there are no obstacles between them.

Alice want to know a maximal number of rooks which she can put on the board without bad pairs. Help her calculate this number.

You have to solve this problem for several test cases.

\(INPUT\)

The first line of the input contains a single integer \(tests\) - a number of test cases.

Each test case is given in the following format:

The first line contains a three integers n, m and k (\(1 \leq n, m \leq 500\), \(0 \leq k < n \cdot m\)) - a number of rows, a number of columns and a number of obstacles.

Then follow k lines with obstacles description. The i-th of these lines contains two integers \(r_i\) and \(c_i\) (\(1 \leq r_i \leq n\), \(1 \leq c_i \leq m\)) - a row number of i-th obstacle and a column number of i-th obstacle.

Guaranteed that all obstacles are placed in the different cells.

Guaranteed that \(\sum_{i=1}^{tests} n_i \cdot m_i \leq 250000\).

\(OUTPUT\)

For each test case print a single integer number - an answer of the problem.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
Tags:
MathematicsHardOpenApproved
Points:20
38 votes
Tags:
ApprovedEasyGeometryMathOpen
Points:30
8 votes
Tags:
Binary search algorithmMediumSearching algorithmAlgorithmsBinary Searchbinary indexed treeBinary indexed tree