아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Grid

시간 제한2초메모리 제한1024 MB

요약
거대한 격자에서 이미 막힌 칸들이 주어질 때, 빈 칸들이 두 개 이상의 연결 영역으로 나뉘도록 추가로 막아야 하는 칸 수의 최솟값을 구하고, 불가능하면 -1을 출력한다.
난이도

보통10점 중 7점

유형
그래프, BFS, 구현, 기하
정답자
아직 제출이 없습니다

문제

There is an n×mn \times m grid where cc (0≤c≤nm)(0 \leq c \leq nm) cells are 1 cells and the rest of the cells are 0 cells.

We say two cells sharing an edge are adjacent. We say two 0 cells are connected if and only if the two 0 cells are adjacent or there exists the third 0 cell that is connected with both cells.

Now we want to replace some (zero, one, or multiple) 0 cells by 1 cells so that after the replacement there exists two zero cells that are not connected with each other.

For example, if there is a 4×44 \times 4 grid where the top-left and the bottom-right corners are 1 cells and the rest are 0 cells, then it is optimal to replace 2 0 cells by 1 cells so that there exists two 0 cells that are disconnected from each other: for example, one possible solution is to replace the other two 0 cells on the diagonal connecting the top-left and the bottom-right corners.

You need to check if the goal can be achieved. If the goal can be achieved, you need to output the minimum number of 0 cells that should be made into 1 cells.

입력

Each test input consists of multiple test cases. The first line of the input file consists of an integer TT denoting the number of test cases in the input file. TT test cases follow.

The first line of each test case consists of three integers n,m,cn,m,c. In the following cc lines, each line consists of two integers x,yx,y denoting the cell on the xx-th row and the yy-th column is an 1 cell. The same 1 cell won't appear multiple times in the same test case.

Two neighboring integers in a line is separated by a space.

출력

For each test case, output a line denoting the answer.

If in the test case, it is impossible to disconnect two 0 cells, output -1. Otherwise, output the minimum number of 0 cells that should be replaced by 1 cells.

힌트

For all test cases, 1≤n,m≤1091 \leq n,m \leq 10^9, 0≤c≤min⁡(nm,105),1≤x≤n,1≤y≤m,1≤T≤200 \leq c \leq \min(nm,10^5), 1 \leq x \leq n, 1 \leq y \leq m, 1 \leq T \leq 20.

Let ∑c\sum c denote the sum of cc among the TT test cases in an input file. It is guaranteed that ∑c≤105\sum c \leq 10^5.

예제1

  1. 예제 1

    입력
    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