#K35442. Minimum Number of Barriers

    ID: 25532 Type: Default 1000ms 256MiB

Minimum Number of Barriers

Minimum Number of Barriers

This problem asks you to determine the minimum number of barriers needed to cover all the entry points in a grid. For each test case, you are given the dimensions of a grid (width W and height H), the number of entry points N, and N points with their coordinates. The goal is to cover all entry points using barriers aligned either vertically or horizontally. To minimize the number of barriers, you should choose the direction (vertical or horizontal) that requires fewer lines. Mathematically, the answer for a test case is given by:

$$\min(|\{x_i\}|,|\{y_i\}|)$$

where \(|\{x_i\}|\) is the number of unique x-coordinates and \(|\{y_i\}|\) is the number of unique y-coordinates of the entry points.

inputFormat

The input is read from stdin and is structured as follows:

  1. The first line contains an integer T, the number of test cases.
  2. Each test case begins with a line containing three integers: W, H, and N, representing the grid's width, height, and the number of entry points.
  3. This is followed by N lines, each containing two integers, representing the x and y coordinates of an entry point.

outputFormat

For each test case, output a single integer on a new line representing the minimum number of barriers needed to cover all the entry points. The output is printed to stdout.

## sample
2
5 5 4
1 1
1 3
4 3
4 1
10 10 3
2 2
8 8
5 5
2

3

</p>