Codeforces Round #500 (Div. 2) D - Chemical table
首先我们如果满足三缺一,那么必有同行和同列的点
如果两行有同列的数,我们可以设想,他们最后会全部填充成为两者啥都有的情况
显然这个是个并查集
现在我们有了很多集合,每个集合自己可以进行三缺一操作,但是集合有缺陷,集合里面的人都没有的列数,那就没法搞
可以贪心的想,一共k个集合的话,把k个集合连接起来需要k-1个新点,如果还有列没有,那就需要这些列需要新店
除此之外,一开始没有讨论有些行压根没有点,这些行也需要点去开辟疆土
综上所述,一开始论述的时候将行和列交换也是合理的
人总喜欢在伤心,劳累,挫折时放纵自己,但这之后把你引向深渊
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 200005;
const int INF = 0x3f3f3f3f;
int f[N]; //col
int find(int x) { return f[x] == x ? x : (f[x] = find(f[x])); }
vector<int> row[N];
vector<int> col[N];
vector<int> part[N];
int has[N];
int tag[N];
int main() {
int n, m, q;
while(~scanf("%d %d %d", &n, &m, &q)) {
for(int i = 1; i <= m; ++i) f[i] = i;
for(int i = 0; i < q; ++i) {
int a, b;
scanf("%d %d", &a, &b);
row[a].push_back(b);
tag[a] ++;
col[b].push_back(a);
has[b] = 1;
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j < row[i].size(); ++j) {
int t1 = row[i][0];
int t2 = row[i][j];
int f1 = find(t1); int f2 = find(t2);
if(f1 != f2) {
f[f2] = f1;
}
}
}
for(int i = 1; i <= m; ++i) {
int tt = find(i);
part[tt].push_back(i);
}
int cnt = 0; int cntPart = 0;
for(int i = 1; i <= m; ++i) {
if(!has[i]) continue;
if(part[i].size() > 0) {
cntPart ++;
cnt += part[i].size();
}
}
int result = 0;
for(int i = 1; i <= n; ++i) {
if(!tag[i])
result ++;
}
// printf("%d\n", cnt);
printf("%d\n", result + cntPart - 1 - cnt + m);
}
return 0;
}
Codeforces Round #500 (Div. 2) D - Chemical table的更多相关文章
- Codeforces Round #500 (Div. 2) [based on EJOI]
Codeforces Round #500 (Div. 2) [based on EJOI] https://codeforces.com/contest/1013 A #include<bit ...
- Codeforces Round 500 (Div 2) Solution
从这里开始 题目地址 瞎扯 Problem A Piles With Stones Problem B And Problem C Photo of The Sky Problem D Chemica ...
- Codeforces Round #323 (Div. 2) C. GCD Table 暴力
C. GCD Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/583/problem/C ...
- Codeforces Round #323 (Div. 2) C. GCD Table map
题目链接:http://codeforces.com/contest/583/problem/C C. GCD Table time limit per test 2 seconds memory l ...
- Codeforces Round #323 (Div. 2) C.GCD Table
C. GCD Table The GCD table G of size n × n for an array of positive integers a of length n is define ...
- Codeforces Round #323 (Div. 1) A. GCD Table
A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #140 (Div. 1) D. The table 构造
D. The table 题目连接: http://www.codeforces.com/contest/226/problem/D Description Harry Potter has a di ...
- Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题
A. Multiplication Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/57 ...
- Codeforces Round #256 (Div. 2) D. Multiplication Table(二进制搜索)
转载请注明出处:viewmode=contents" target="_blank">http://blog.csdn.net/u012860063?viewmod ...
随机推荐
- 关于"XML 文档(2, 2)中有错误:不应有 <xml xmlns=''>"错误
XML文件名 <?xml version="1.0" encoding="utf-8"?> <Config xmlns:xsi="h ...
- jquery全选 反选
//全选 反选 $('#chkAll').on('click',function(){ $('input.chkbox').prop('checked',$(this).prop('checked') ...
- 【luogu P1606 [USACO07FEB]荷叶塘Lilypad Pond】 题解
题目链接:https://www.luogu.org/problemnew/show/P1606 这个题..第一问很好想,但是第二问,如果要跑最短路计数的话,零边权的花怎么办? 不如这样想,如果这个点 ...
- CSP 试题编号201803-1 Java实现
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner input ...
- ABAP术语-URL
URL 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/20/1114193.html Uniform Resource Locator (U ...
- linux查看网卡地址和硬盘序列号
linux查看网卡地址命令:ifconfig linux查看硬盘序列号命令:hdparm -i /dev/sda
- 点击label执行两次的bug
首先我是这样的写的: <label> <input type="checkbox">点击 </label> 无论点击input还是文字,都会执行 ...
- gulp之几个常用插件介绍
今天给大家分享一篇gulp几款插件的使用 以下代码用到得模块加载‘ const gulp=require("gulp"); const gulpSass=require(" ...
- react-navigation的超级大坑
本文针对react-navigation^3.0.0版本,版本不对的话,请不要看本文,直接看官方英文文档 最近一直在学习RN,没找到什么好的视频,所以一直看文档,一路上来虽然遇到一些乱七八糟的bu ...
- iPhone Plus手机的分辨率到底是多少,是1080×1920还是1242×2208?
近日在准备AppStore上架的时候,需要提供屏幕快照,苹果官方的要求是: 5.5寸的iOS设备的分辨率是:是1080×1920:然而我们如果找一张Plus的屏幕截图,会发现截图的分辨率是1242×2 ...