codeforces 1012b Chemical table

B. Chemical table

time limit per test

1 second

memory limit per test

512 megabytes

input

standard input

output

standard output

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 (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2).

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.

Input

The first line contains three integers nmq (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 rici (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.

Output

Print the minimal number of elements to be purchased.

Examples

input

Copy

2 2 3
1 2
2 2
2 1

output

Copy

0

input

Copy

1 5 3
1 3
1 1
1 5

output

Copy

2

input

Copy

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

output

Copy

1

题目大意:一个n*m的矩形初始有q个元素,对于一个组成矩形的四个点中如果有三个点中有元素,那么第四个点中会产生新的元素,问需要再加多少个元素才能填满整个n*m的矩形。

整个矩形可以看作一个二分图, 左边n个点, 右边m个点, 对于一个点(x,y), 可以看作x->y的边。 然后对于一条路径x->y, 如果x,y有边, 说明原来就有元素, 无须添加, 否则 一定是长度>=3的边数为奇数的交错路径, 接着考虑前三个边x->a->b->c, 根据题目要求, 可以生成x->c这条边, 因此交错路径长度-2, 一直做下去, 会直接产生x->y这条边。 故对于一个联通块而言, 其中所有的边都可以产生,即其中的所有元素都可以产生出来,所以答案就是联通分量数-1。
 

#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 5e5;
int n, m, q;
vector<int> G[maxn];
int vis[maxn];

void dfs(int x) {
    vis[x] = 1;
    for (int i = 0; i < G[x].size(); i++)
        if (!vis[G[x][i]]) dfs(G[x][i]);
}

int main() {
    scanf("%d%d%d", &n, &m, &q);
    int x, y;
    for (int i = 1; i <= q; i ++) {
        scanf("%d%d", &x, &y);
        G[x].push_back(y + n);
        G[y + n].push_back(x);
    }
    int cnt = 0;
    for (int i = 1; i <= (n + m); i++) {
        if (!vis[i]) {
            dfs(i);
            cnt ++;
        }
    }
    printf("%d\n", cnt - 1);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值