시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 512 MB72232342.593%

문제

Innopolis University scientists continue to investigate the periodic table. There are n · m known elements and they form a periodic table, a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table. Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to sides of the table, if they have samples of three of four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).

Original samples of elements as well as newly crafted elements can be used again in future fusions.

Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using arbitrary number of nuclear fusions in some order. Help them find the minimal number of elements they need to purchase.

입력

First line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000))—chemical table dimensions and the number of elements scientists already have. Following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m) each—descriptions of the elements that scientists already have. All elements in the input are different.

출력

In the only line print k—the minimal number of elements to be purchased.

서브태스크 1 (10점)

  • n = 2
  • m = 2
  • 0 ≤ q ≤ 4

서브태스크 2 (8점)

  • n = 1
  • 1 ≤ m ≤ 20
  • 0 ≤ q ≤ 20

서브태스크 3 (9점)

  • n = 2
  • 1 ≤ m ≤ 20
  • 0 ≤ q ≤ 40

서브태스크 4 (8점)

  • 1 ≤ n ≤ 20
  • 1 ≤ m ≤ 20
  • q = 0

서브태스크 5 (20점)

  • 1 ≤ n ≤ 20
  • 1 ≤ m ≤ 20
  • 0 ≤ q ≤ 400

서브태스크 6 (10점)

  • 1 ≤ n ≤ 100
  • 1 ≤ m ≤ 100
  • 0 ≤ q ≤ 10 000

서브태스크 7 (10점)

  • 1 ≤ n ≤ 250
  • 1 ≤ m ≤ 250
  • 0 ≤ q ≤ 62 500

서브태스크 8 (10점)

  • 1 ≤ n ≤ 10 000
  • 1 ≤ m ≤ 10 000
  • 0 ≤ q ≤ 100 000

서브태스크 9 (15점)

  • 1 ≤ n ≤ 200 000
  • 1 ≤ m ≤ 200 000
  • 0 ≤ q ≤ 200 000

예제 입력 1

2 2 3
1 2
2 2
2 1

예제 출력 1

0

예제 입력 2

1 5 3
1 3
1 1
1 5

예제 출력 2

2

예제 입력 3

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

예제 출력 3

1

힌트

Pictures below explain examples.

The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.

The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from another labs (optimal solution should minimize number of red circles). Blue dashed circles are elements which can be produced with nuclear fusion. They are numbered starting in order in which they can be produced.

Test 1

We can use nuclear fusion and get the element from other three samples, so we don’t need to purchase anything.

Test 2

We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements.

Test 3

Note that after purchasing one element it’s still not possible to produce the middle element in the top row (marked as 4). So we produce the element in the left-bottom corner first (marked as 1), and then use it in future fusions.

채점 및 기타 정보

  • 예제는 채점하지 않는다.