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 ...
随机推荐
- 【CSU 1803】2016 (数学)
Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input 输入包含不超过 30 ...
- BZOJ 5248: [2018多省省队联测]一双木棋(对抗搜索)
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 439 Solved: 379[Submit][Status][Discuss] Descriptio ...
- MySQL语句详解(用户表、密码、权限、远程)
注: mysql.user表中Host为%的含义 Host列指定了允许用户登录所使用的IP,比如user=root Host=192.168.1.1.这里的意思就是说root用户只能通过192.168 ...
- centos6 命令界面切换到图形界面
要进入图形界面,首先要安装.所以应该先执行 yum groupinstall "X Window System" -y yum groupinstall "Desktop ...
- Nginx与Tomcat实现请求动态数据与请求静态资源的分离
上篇博客说明了Nginx在应用架构中的作用,以及负载均衡的思路.这篇实践一下其中的访问静态资源与访问动态资源的操作. 一.认识访问静态资源与访问动态资源的区别 静态资源:指存储在硬盘内的数据,固定的数 ...
- jQuery实现全选、不选和反选功能
jQuery结合Font Awesome字体图标实现全选.不选和反选功能 Font Awesome字体图标链接地址:http://www.fontawesome.com.cn/faicons/ 效果: ...
- jdk8新特性-stream
一.什么是流stream 1.可理解为高级版本的 Iterator 不是集合元素,它不是数据结构并不保存数据,它是有关算法和计算的. 2.单向,不可往复 数据只能遍历一次,遍历过一次后即用尽了,就好比 ...
- SVN(独立安装)-1.9.7 centos 6.5(64位)
说明: 运行方式: 基于Apache的http.https网页访问形式: 基于svnserve的独立服务器模式. 数据存储方式: 在Berkeley DB数据库中存储数据: 使用普通的文件FSFS存储 ...
- 记遇到的一个php坑
最近对项目的一个高访问量业务接口进行功能扩展,上线一段时间后,服务器cpu load突然飙升,并出现大量502.一开始找运维查看日志,并没有看是什么问题,后来发现别的部门项目之前也遇到类似的问题,原来 ...
- C#串口通信及数据表格存储
1.开发环境 系统:win10 开发工具:Visual Studio 2017 2.界面设计 串口通信的界面大致如此,在此基础上添加项目所需的调试指令与数据存储功能,界面排布方面可参考其他教程. 3. ...