A11907 | Chemical table
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
The first line contains three integers $n$ , $m$ , $q$ ( $1<=n,m<=200000$ ; $0<=q<=min(n·m,200000)$ ), 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.
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.
输出格式
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.

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.

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted