题目链接:https://atcoder.jp/contests/abc131/tasks/abc131_f

转自博客:https://blog.csdn.net/qq_37656398/article/details/93496476

题目大意

  给定 N 个点,若有如下图所示的三个点(黑点)就可以构造岀一个新的点(红点)。求可构造的点的最多的个数。

分析

  首先,如果两个点的某一个坐标相等,我们定义这两个点是联通的。
  其次,如果红点能够被构造,它必然是由处于同一连通块中的点所构造的。
  于是,只要分别计算每个联通块能构造多少点就可以了。
  对于每个连通块,把 x 坐标和 y 坐标单独弄一个集合,然后就在两个集合中不断分别取两个进行构造,直到不能构造为止,图示如下:
  我们发现,构造的过程就是把构造前的二部图构造成完全二部图,每多一条边就表明构造了一个点。
  于是把所有连通块所对应的完全二部图边数都加起来再减去 N 就能得到能构造的点的数目了。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef set< PII > SPII;
typedef vector< int > VI;
typedef vector< double > VD;
typedef vector< VI > VVI;
typedef vector< SI > VSI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, string > MIS;
typedef map< int, PII > MIPII;
typedef map< PII, int > MPIII;
typedef map< string, int > MSI;
typedef map< string, string > MSS;
typedef map< PII, string > MPIIS;
typedef map< PII, PII > MPIIPII;
typedef multimap< int, int > MMII;
typedef multimap< string, int > MMSI;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; int N, K, M;
LL ans; int f[maxN << ], sz[maxN << ][]; int Find(int x){
while (x != f[x]) x = f[x] = f[f[x]];
return x;
} int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
//INIT();
cin >> N;
Rep(i, maxN << ) f[i] = i; Rep(i, N) {
int x, y;
cin >> x >> y;
y += maxN;
f[Find(x)] = Find(y);
}
Rep(i, maxN << ) ++sz[Find(i)][i / maxN];
Rep(i, maxN << ) ans += (LL)sz[i][] * sz[i][];
cout << ans - N << endl;
return ;
}

AtCoder ABC 131F Must Be Rectangular!的更多相关文章

  1. ATCODER ABC 099

    ATCODER ABC 099 记录一下自己第一场AK的比赛吧...虽然还是被各种踩... 只能说ABC确实是比较容易. A 题目大意 给你一个数(1~1999),让你判断它是不是大于999. Sol ...

  2. Atcoder ABC 141

    Atcoder ABC 141 A - Weather Prediction SB题啊,不讲. #include<iostream> #include<cstdio> #inc ...

  3. Atcoder ABC 139E

    Atcoder ABC 139E 题意: n支球队大循环赛,每支队伍一天只能打一场,求最少几天能打完. 解法: 考虑抽象图论模型,既然一天只能打一场,那么就把每一支球队和它需要交手的球队连边. 求出拓 ...

  4. Atcoder ABC 139D

    Atcoder ABC 139D 解法: 等差数列求和公式,记得开 $ long long $ CODE: #include<iostream> #include<cstdio> ...

  5. Atcoder ABC 139C

    Atcoder ABC 139C 题意: 有 $ n $ 个正方形,选择一个起始位置,使得从这个位置向右的小于等于这个正方形的高度的数量最多. 解法: 简单递推. CODE: #include< ...

  6. Atcoder ABC 139B

    Atcoder ABC 139B 题意: 一开始有1个插口,你的插排有 $ a $ 个插口,你需要 $ b $ 个插口,问你最少需要多少个插排. 解法: 暴力模拟. CODE: #include< ...

  7. Atcoder ABC 139A

    Atcoder ABC 139A 题意: 给你两个字符串,记录对应位置字符相同的个数 $ (n=3) $ 解法: 暴力枚举. CODE: #include<iostream> #inclu ...

  8. atcoder abc 244

    atcoder abc 244 D - swap hats 给定两个 R,G,B 的排列 进行刚好 \(10^{18}\) 次操作,每一次选择两个交换 问最后能否相同 刚好 \(10^{18}\) 次 ...

  9. AtCoder ABC 250 总结

    AtCoder ABC 250 总结 总体 连续若干次一样的结果:30min 切前 4 题,剩下卡在 T5 这几次卡在 T5 都是一次比一次接近, 什么 dp 前缀和打挂,精度被卡,能水过的题连水法都 ...

随机推荐

  1. LeetCode Linked List Medium 2. Add Two Numbers

    Description You are given two non-empty linked lists representing two non-negative integers. The dig ...

  2. 【知识强化】第五章 传输层 5.3 TCP协议

    这节课我们来学习一下TCP协议的特点以及TCP报文段的格式. 首先呢我们来看一下TCP有哪些特点呢.之前我们说过TCP它是一个比较可靠的面向连接的协议,所以最主要的特点它是可以面向连接的一种传输层协议 ...

  3. MongoDB--副本集基本信息

    副本集的概念 副本集是一组服务器,其中有一个是主服务器(primary),用于处理客户端请求:还有多个备份服务器(secondary),用于保存主服务器的数据副本.如果主服务器崩溃了,备份服务器会自动 ...

  4. C++判断字符是否是元音字母

    写这个随笔的起因很奇怪. 我本来想找找C++有没有内置的函数(类似isalpha(), isdigit(), isalnum()之流)能直接完成这个功能,但是函数没发现,却发现很多博客都是逐个字符判断 ...

  5. Oracle之子查询:Top-N问题

    学习了SQL子查询,遇到个Top-N问题,即:加入有张工资表(这里使用Oracle SCOTT用户的emp表),需要查找工资最高的3个员工信息,以下列格式输出: 乍眼一看,这很简单啊,对sal进行排序 ...

  6. 在脚本中使用set命令调试脚本

    当脚本文件较长时,可以使用set命令指定调试一段脚本.在脚本中使用set -x命令开启调式模式:使用set +x命令关闭调式模式. 例如: #!/bin/bash #Scriptname: greet ...

  7. 力扣—climbing stairs(爬楼梯) python实现

    题目描述: 中文: 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 英文: You are cl ...

  8. 人脸识别课件需要安装的python模块

    Python3.6安装face_recognition人脸识别库 https://www.jianshu.com/p/8296f2aac1aa

  9. 数据库索引原理,及MySQL索引类型(转)

    在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytable表: CREATE TABLE mytable( ID INT NOT NULL, username ) NOT N ...

  10. sqoop 数据导入hive

    一. sqoop: mysql->hive sqoop import -m 1 --hive-import --connect "jdbc:mysql://127.0.0.1:3306 ...