luogu#P5089. [eJOI 2018] 元素周期表

[eJOI 2018] 元素周期表

Background

This problem is translated from eJOI2018 Problem D “Chemical table”.

Problem Description

Professors at Innopolis University are working hard on studying the periodic table of elements. They know that there are n×mn \times m elements, forming a matrix with nn rows and mm columns.

Research shows that if there is an element AA in the periodic table, element BB is in the same column as it (but AA and BB cannot be in the same row), and element CC is in the same row as it (but AA and CC cannot be in the same column), then scientists can use these three elements to produce a sample of a fourth element DD by nuclear fusion. Element DD is in the same row as BB and in the same column as CC.
In short, if there are samples of three elements located at (r1,c1), (r1,c2), (r2,c1)(r_1, c_1),\ (r_1, c_2),\ (r_2, c_1) in the periodic table (where r1r2,c1c2r_1 \neq r_2, c_1 \neq c_2), then a sample at (r2,c2)(r_2, c_2) can be produced, as shown in the figure:

Note: The samples used in nuclear fusion do not disappear; they can take part in later reactions. Samples obtained from reactions can also take part in reactions.

They have already obtained samples of qq elements. To collect samples of all elements, they will buy some samples and then use nuclear fusion to produce the remaining ones.
Please find the minimum number of element samples they need to buy.

Input Format

The first line contains 33 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 next qq lines each contain 22 integers ri,ci (1rin,1cim)r_i, c_i \ (1 \le r_i \le n, 1 \le c_i \le m). It is guaranteed that all given elements are distinct.

Output Format

Output one integer, the minimum number of element samples that need to be bought.

2 2 3
1 2
2 2
2 1
0
1 5 3
1 3
1 1
1 5
2
4 3 6
1 2
1 3
2 2
2 3
3 1
3 3
1

Hint

Sample Explanation

Notes

In each sample explanation, there are two matrices.
The first shows the initial state (where crosses mark the elements that already have samples).
The second shows the final state when all samples have been collected (where blue circles represent samples obtained by nuclear fusion, the number inside a blue circle is the order in which it was obtained, and red circles represent purchased samples).

Sample Explanation 1

With the three given elements, a sample of the fourth element can be obtained.

Sample Explanation 2

Since the given elements are only in one row, nuclear fusion cannot be used, so the remaining two element samples can only be purchased.

Sample Explanation 3

The method for collecting all elements is not unique; the following is one possible method. In this method, element (4,2)(4, 2) can only be obtained after purchasing a sample of element (4,1)(4, 1) and obtaining a sample of element (1,1)(1, 1) from reactions.


Subtasks

Note: You will receive the score for a subtask if and only if you pass all test points in that subtask.

Subtask ID Score nn mm qq
11 1010 n=2n=2 m=2m=2 0q40 \le q \le 4
22 1717 1n21 \le n \le 2 1m201 \le m \le 20 0q200 \le q \le 20
33 88 1n201 \le n \le 20 q=0q=0
44 2020 0q4000 \le q \le 400
55 3030 1n1×1041 \le n \le 1 \times 10^4 1m1×1041 \le m \le 1 \times 10^4 1q1×1051 \le q \le 1 \times 10^5
66 1515 1n2×1051 \le n \le 2 \times 10^5 1m2×1051 \le m \le 2 \times 10^5 1q2×1051 \le q \le 2 \times 10^5

Translated by ChatGPT 5