#P1173. [NOI2016] 网格

    ID: 173 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>贪心高精度2016NOI 系列O2优化枚举,暴力

[NOI2016] 网格

Description

The Flea King and the Cricket King are playing a game.

They deploy pieces on an nn-row, mm-column grid. Among the cc cells (0cnm0 \leq c \leq n \cdot m), each such cell contains a cricket, and each remaining cell contains a flea.

Two fleas are called adjacent if their occupied cells share a side.

Two fleas are called connected if and only if they are adjacent, or there exists a sequence of fleas starting from one and ending at the other such that consecutive fleas in the sequence are adjacent.

Now, the Cricket King wants to replace some fleas (zero, one, or more) with crickets so that afterwards there exist at least two fleas that are not connected.

For example: Figure 11 describes a case with n=4n=4, m=4m=4, c=2c=2.

In this case, the Cricket King can replace the fleas at row 22, column 22 and row 33, column 33 with crickets to achieve his goal, as shown in the right picture. Moreover, no better plan exists, although there may be other plans that also replace two fleas.

You must first determine whether the Cricket King's goal can be achieved. If it can, you must also minimize the number of fleas replaced.

Input Format

Each input file contains multiple test cases.

The first line contains a single integer TT, the number of test cases.

For each of the next TT test cases, the first line contains three integers n,m,cn, m, c.

Then cc lines follow. Each line contains two integers x,yx, y indicating that the cell in row xx, column yy is occupied by a cricket. Within each test case, no cricket cell is listed more than once.

Output Format

For each test case, output one line.

If the Cricket King's goal cannot be achieved for this test case, output 1-1. Otherwise, output the minimum number of fleas that must be replaced.

4
4 4 2
1 1
4 4
2 3 1
1 2
2 2 2
1 1
2 2
1 1 0
2
1
0
-1

Hint

Sample explanation

  • The first test case is the example in the statement.
  • For the second test case, replacing the flea at row 22, column 22 makes two fleas not connected, and no better plan exists.
  • For the third test case, there already exist two fleas that are not connected initially, so no replacements are needed.
  • For the fourth test case, since there is at most one flea, no matter how you replace, there cannot exist two fleas that are not connected.

Constraints

For all test points, it is guaranteed that 1T201 \leq T \leq 20. Let c\sum c denote, within one test point, the sum of cc over its TT test cases. For all test points, c105\sum c \leq 10^5.

For all data, 1n,m1091 \leq n, m \leq 10^9, 0cn×m0 \leq c \leq n \times m, 1xn1 \leq x \leq n, 1ym1 \leq y \leq m.

The detailed constraints for each test point are shown in the table below. In the table, n,m,cn, m, c refer to a single test case (not a test point), i.e., all TT test cases within the same test point meet the listed limits; and c\sum c is for a single test point. For readability, the “Test Point” column is placed in the middle rather than on the left.

n,mn,m Test Point cc
n×m4n\times m\leq 4 11 cn×mc\leq n\times m
n×m8n\times m\leq 8 22 ^
n×m15n\times m\leq 15 33
n×m30n\times m\leq 30 44
n×m100n\times m\leq 100 55
n×m300n\times m\leq 300 66
n×m103n\times m\leq 10^3 77
n×m2×104n\times m\leq 2\times 10^4 88 c5c\leq 5
^ 99 c15c\leq 15
1010 c30c\leq 30
n,m2×104,n×m2×104n,m\leq 2\times 10^4,n\times m\leq2\times 10^4 1111 c2×104\sum c\leq 2\times 10^4
n,m2×104,n×m105n,m\leq 2\times 10^4,n\times m\leq10^5 1212 ^
n,m2×104,n×m3×105n,m\leq 2\times 10^4,n\times m\leq3\times 10^5 1313
n,m2×104,n×m106n,m\leq 2\times 10^4,n\times m\leq10^6 1414
n,m2×104,n×m109n,m\leq 2\times 10^4,n\times m\leq 10^9 1515
n,m105n,m\leq 10^5 1616 c105\sum c\leq 10^5
n,m109n,m\leq 10^9 1717 c=0c=0
^ 1818 c1c\leq 1
1919 c2c\leq 2
2020 c3c\leq 3
2121 c10c\leq 10
2222 c30c\leq 30
2323 c300c\leq 300
2424 c2×104\sum c\leq 2 \times 10^4
2525 c105\sum c\leq 10^5

Translated by ChatGPT 5