给定的一个n*m的区域内,给出一些点的坐标,这些点上有一个元素,如果在矩形的子矩形的三个点都有元素,那么第四个点的元素可以自己产生,其他的元素需要购买,问最少需要购买多少中元素才可以把这个区域给填满。

对于给出点的,先用并查集把x轴上和y轴上有联系的点都联系起来,并且可以顺便把出现过的x和y的数值标记起来。

如果我把所有的点分成了n块,那么的需要添加n-1个元素来把这n块连接起来。对于没有出现的x和y的数值,我需要额外买一个元素来填充这个点。

因为我已经在一个块内的点一定是有联系的,而另一个块的点一定与这里面的点都没有联系,所以我只需要添加一个元素让他们的 x 或者 y 联系起来,那么这个块一定可以形成一个包含三个点的子矩形,也就可以产生出另一个点的元素。就可以把这些块里出现的x和y的横纵方向填满,这样就会剩下那么没有出现的x和y位置上的点还是空的了,然后同样的也只需要添加一个就可以填满一行或者一列。

#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lowbit(x) (x & (-x)) typedef unsigned long long int ull;
typedef long long int ll;
const double pi = 4.0*atan(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = ;
const int maxm = ;
const int mod = ;
using namespace std; int n, m, tol, T;
int fa[maxn];
int X[maxn];
int Y[maxn]; int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
} void build(int x, int y) {
int u = find(x);
int v = find(y);
fa[v] = u;
} int main() {
int q;
scanf("%d%d%d", &n, &m, &q);
for(int i=; i<=q; i++) fa[i] = i;
int x, y;
for(int i=; i<=q; i++) {
scanf("%d%d", &x, &y);
if(X[x]) build(i, X[x]);
X[x] = fa[i];
if(Y[y]) build(i, Y[y]);
Y[y] = fa[i];
}
ll ans = -;
for(int i=; i<=q; i++)
find(i);
for(int i=; i<=q; i++)
if(fa[i] == i) ans++;
for(int i=; i<=n; i++)
if(!X[i]) ans++;
for(int i=; i<=m; i++)
if(!Y[i]) ans++;
printf("%I64d\n", ans);
return ;
}

Chemical table CFR500 div2D(并查集)的更多相关文章

  1. codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集

    C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...

  2. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  3. codeforces 651E E. Table Compression(贪心+并查集)

    题目链接: E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  4. Codeforces #345div1 C Table Compression (650C) 并查集

    题意:给你一个n*m的矩阵,需要在不改变每一行和每一列的大小关系的情况下压缩一个矩阵,压缩后的矩阵所有数的总和尽量的小. 思路:我们有这样的初步设想:对于在一行或一列的数x,y,若x<y,则建立 ...

  5. Codeforces Round #345 (Div. 2) E. Table Compression(并查集)

    传送门 首先先从小到大排序,如果没有重复的元素,直接一个一个往上填即可,每一个数就等于当前行和列的最大值 + 1 如果某一行或列上有重复的元素,就用并查集把他们连起来,很(不)显然,处于同一行或列的相 ...

  6. Codeforces 651E Table Compression【并查集】

    题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: ...

  7. Code Forces 650 C Table Compression(并查集)

    C. Table Compression time limit per test4 seconds memory limit per test256 megabytes inputstandard i ...

  8. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  9. Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题

    E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. linux之常见错误

    在日常开发中,尤其是在Linux中进行操作的时候,经常会碰到各种各样的错误.记录一下,熟能生巧,慢慢参透linux的奥秘 1) 在安装ssl证书的时候,发生certbot命令无法使用的情况 解决方案: ...

  2. React Native之微信分享(iOS Android)

    React Native之微信分享(iOS Android) 在使用React Native开发项目的时候,基本都会使用到微信好友或者微信朋友圈分享功能吧,那么今天我就带大家实现以下RN微信好友以及朋 ...

  3. js中获取当前项目名等

    实际上通过window.location可以获取很多跟资源路径相关的信息,需要用到的时候直接通过浏览器调试可以查看window.location的一些属性

  4. Kettle中表输出字段和字段选择

    表输出: 字段选择: 注:字段选择可以输出匹配后的选中列,表输出则输出匹配后的所有列.

  5. Java使用RabbitMQ之公平分发

    发送消息: package org.study.workfair; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Con ...

  6. Lodop打印维护PRINT_SETUP本地缓存ini文件

    针对千差万别的客户端,Lodop提供了打印维护(PRINT_SETUP),可以针对某个客户端微调,调整结果保存在客户端本地,不会影响其他访问网站的用户的使用. 打印维护使用方法:1.PRINT_INI ...

  7. LODOP打印安装到win的特殊字体

    LODOP能够打印的字体,来源于安装到本机windows里字体库的字体,如果需要打印特别的字体,需要在该操作系统安装.由于web网站的用户千差万别,字体库也有不同,但是一般常见的字体都是有的,因此做模 ...

  8. NPOI 上传Excel功能(三)

    4.验证Excel并上传 using DC.BE.Business.SAS; using DC.BE.Business.SYS; using DC.BE.Entity.SAS; using DC.BE ...

  9. Sql server 系统表

    sql server系统表详细说明 SQL Server 用户库中系统表说明 名称 说明 备注 syscolumns 每个表和视图中的每列在表中占一行,存储过程中的每个参数在表中也占一行.   sys ...

  10. Windows 7 SP1 x64 LSP

    NALapi.dll napinsp.dll pnrpnsp.dll mswsock.dll winrnr.dll