CF1012B.Chemical table

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (_r_1, _c_1), (_r_1, _c_2), (_r_2, _c_1), where _r_1 ≠ _r_2 and _c_1 ≠ _c_2, then we can produce element (_r_2, _c_2).

Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used 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 an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.

Innopolis 大学的科学家们持续对元素周期表展开研究。目前已知有 n⋅mn \cdot m 种元素,它们构成一张周期表:一个具有 nn 行和 mm 列的矩形表格。每种元素均可由其在表中的坐标 (r, c)(r,\,c)(其中 1≤r≤n1 \le r \le n,1≤c≤m1 \le c \le m)来描述。

最近,科学家们发现:对于该表中任意四个能构成一个边与表格边界平行的矩形的不同元素,若已拥有其中三个元素的样品,则可通过核聚变反应制备出第四个元素的样品。因此,若已拥有位于 (r1, c1)(r_1,\,c_1)、(r1, c2)(r_1,\,c_2) 和 (r2, c1)(r_2,\,c_1) 的元素(其中 r1≠r2r_1 \ne r_2 且 c1≠c2c_1 \ne c_2),则可制备出位于 (r2, c2)(r_2,\,c_2) 的元素。

核聚变过程中所用的样品不会被消耗,可重复用于后续的聚变反应;新制备出的元素样品亦可用于后续的聚变反应。

Innopolis 大学的科学家们目前已拥有 qq 种元素的样品。他们希望获得全部 n⋅mn \cdot m 种元素的样品。为此,他们将从其他实验室购买若干样品,然后通过任意次数、任意顺序的核聚变反应来制备其余所有元素。请帮助他们求出所需购买的最少元素种类数。

输入格式

The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have.

The following q lines contain two integers r__i, c__i (1 ≤ r__i ≤ n, 1 ≤ c__i ≤ m), each describes an element that scientists already have. All elements in the input are different.

第一行包含三个整数 nn、mm、qq(1≤n,m≤200 0001 \leq n, m \leq 200\,000;0≤q≤min⁡(n⋅m,200 000)0 \leq q \leq \min(n \cdot m, 200\,000)),分别表示化学表格的行数、列数以及科学家已拥有的元素个数。

接下来的 qq 行,每行包含两个整数 rir_i、cic_i(1≤ri≤n1 \leq r_i \leq n,1≤ci≤m1 \leq c_i \leq m),描述科学家已拥有的一种元素。输入中所有元素互不相同。

输出格式

Print the minimal number of elements to be purchased.

输出需要购买的最少元素数量。

输入输出样例

  • 输入#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

说明/提示

For each example you have a picture which illustrates it.

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 other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.

Test 1

We can use nuclear fusion and get the element from three other 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

There are several possible solutions. One of them is illustrated below.

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

每个示例均配有一张示意图。

每个示例的第一张图描述了初始可用的元素样品集合。黑色十字(×)表示实验室最初已有的元素。

第二张图描述了如何获取剩余的样品。红色虚线圆圈表示需从其他实验室采购的元素(最优解应使红色圆圈的数量最少)。蓝色虚线圆圈表示可通过核聚变生成的元素,其编号表示可生成的先后顺序。

测试 1

我们可以利用核聚变,由另外三种样品合成目标元素,因此无需采购任何元素。

测试 2

由于只有一行,完全无法使用核聚变,因此必须采购所有缺失的元素。

测试 3

存在多种可能的解法,其中一种如下图所示。

注意:在采购了一个标为红色的元素后,仍无法立即生成底行中间位置的元素(标为 4)。因此我们首先生成左上角的元素(标为 1),再将其用于后续的聚变反应。

输入解题思路,AI测评打分。不知道怎么写?

首页