P28454 [洛谷] [eJOI2018] Periodic Table
提高
题目描述

Problem Background

This problem is a translation of eJOI2018 Problem D "Chemical Table"

Problem Description

Professors at Innopolis University are diligently studying the periodic table. They know that there are $n \times m$ elements, forming a matrix of $n$ rows and $m$ columns.

Research shows that if there is an element A in the periodic table, and element B is in the same column (A and B are not in the same row), and element C is in the same row (A and C are not in the same column), then scientists can synthesize a sample of a fourth element D through nuclear fusion using these three elements. D is in the same row as B and in the same column as C.
In short, if there are samples of elements at positions $(r_1, c_1)$, $(r_1, c_2)$, $(r_2, c_1)$ (where $r_1 \neq r_2, c_1 \neq c_2$) in the periodic table, a sample at position $(r_2, c_2)$ can be generated. As illustrated below:

Note: The samples used in nuclear fusion do not disappear; they can be used in subsequent reactions; the resulting samples can also participate in reactions.

They have already obtained samples of $q$ elements. To collect samples of all elements, they will purchase some samples and then use nuclear fusion to produce the remaining elements.
Your task is to find out the minimum number of element samples they need to purchase.

Input Format

The first line contains three integers $n, m, q$ $(1 \le n, m \le 2 \times 10^5, 0 \le q \le \min \{n \times m, 2 \times 10^5\})$.
The following $q$ lines each contain two integers $r_i, c_i$ $(1 \le r_i \le n, 1 \le c_i \le m)$. It is guaranteed that the given elements are distinct.

Output Format

Output a single integer, the minimum number of element samples that need to be purchased.

Sample Input and Output

Sample Input #1

2 2 3
1 2
2 2
2 1

Sample Output #1

0

Sample Input #2

1 5 3
1 3
1 1
1 5

Sample Output #2

2

Sample Input #3

4 3 6
1 2
1 3
2 2
2 3
3 1
3 3

Sample Output #3

1

Notes/Hints

Sample Explanation

Explanation

Each sample explanation contains two matrices.
The first represents the initial state (where crossed elements are those with samples initially).
The second represents the state when all samples are collected (where blue circles indicate samples obtained through nuclear fusion, with numbers inside showing the order of acquisition, and red circles indicate purchased samples).

Sample Explanation 1

Using the given three elements, the fourth element's sample can be obtained.

Sample Explanation 2

Since the given elements are all in one row, nuclear fusion cannot be used, so the remaining two elements' samples must be purchased.

Sample Explanation 3

There are multiple ways to collect all elements; the following is one method. Element $(4, 2)$ can only be obtained after purchasing the sample of element $(4, 1)$ and synthesizing the sample of element $(1, 1)$.


Subtasks

Note: You will only receive points for a subtask if you pass all test cases within that subtask.

Subtask Number Points $n$ $m$ $q$
1 10 $n=2$ $m=2$ $0 \le q \le 4$
2 17 $1 \le n \le 2$ $1 \le m \le 20$ $0 \le q \le 20$
3 8 $1 \le n \le 20$ $1 \le m \le 20$ $q=0$
4 20 $1 \le n \le 20$ $1 \le m \le 20$ $0 \le q \le 400$
5 30 $1 \le n \le 1 \times 10^4$ $1 \le m \le 1 \times 10^4$ $1 \le q \le 1 \times 10^5$
6 15 $1 \le n \le 2 \times 10^5$ $1 \le m \le 2 \times 10^5$ $1 \le q \le 2 \times 10^5$
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
2 2 3 1 2 2 2 2 1
样例输出
0