Codeforces 870E Points, Lines and Ready-made Titles 计数
题目链接
题意
给定二维坐标上的\(n\)个点,过每个点可以 画一条水平线 或 画一条竖直线 或 什么都不画,并且若干条重合的直线被看做同一条。问共可能得到多少幅不同的画面?
题解
官方题解
仆の瞎扯
和bzoj 1854的并查集思路蜜汁契合
// 看完了题解的我这样想道
首先显然可以将图分为若干个联通块。
且慢,哪里来的图?
那就先考虑建图?
不急不急,先来想想看每一个联通块的性质。
如果该联通块中有环的话,肯定每条边都能取到;如果联通块是一棵树,那么必有一条边取不到(具体阐述见上bzoj 1854),所以只需要知道
- 这个联通块中有多少条边
- 这个联通块是不是环
这两个信息就可以了
那么可以直接上并查集。
什么样的点可以并到一起呢?横坐标相同的或者纵坐标相同的。
有没有环怎么维护呢?看有没有加进去的边的端点本身就在一个集合里。
联通块中边的数目又怎么知道呢?这倒还挺有意思的,其实只要直接看出现过多少个横坐标或者纵坐标就可以了,因为一个横坐标或者一个纵坐标就代表一条可以选的直线,所以这块联通块的贡献就是\(2^{x+y}或者2^{x+y}-1\)。
然后呢?就做完了。
然而呢?比赛结束。一天了。
然后再推荐一下葫芦爷的题解太强辣
Code
#include <bits/stdc++.h>
#define maxn 100010
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
struct node {
int x, y;
}a[maxn];
int fa[maxn], sz[maxn], f[maxn], id[maxn], m[maxn];
bool circ[maxn], vis[maxn];
vector<int> v[maxn];
set<int> sx, sy;
bool cmp1(int i, int j) {
return a[i].x < a[j].x || (a[i].x == a[j].x && a[i].y < a[j].y);
}
bool cmp2(int i, int j) {
return a[i].y < a[j].y || (a[i].y == a[j].y && a[i].x < a[j].x);
}
LL poww(LL a, LL b) {
LL ret = 1;
while (b) {
if (b & 1) (ret *= a) %= mod;
(a *= a) %= mod;
b >>= 1;
}
return ret;
}
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void unionn(int a, int b) {
a = find(a), b = find(b);
if (a == b) { circ[a] = true; return; }
if (sz[a] > sz[b]) swap(a, b);
fa[a] = b; sz[b] += sz[a];
circ[b] |= circ[a];
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d%d", &a[i].x, &a[i].y);
}
for (int i = 0; i < n; ++i) id[i] = i;
for (int i = 0; i < n; ++i) fa[i] = i, sz[i] = 1;
sort(id, id+n, cmp1);
for (int i = 1; i < n; ++i) {
if (a[id[i]].x == a[id[i-1]].x) unionn(id[i-1], id[i]);
}
sort(id, id+n, cmp2);
for (int i = 1; i < n; ++i) {
if (a[id[i]].y == a[id[i-1]].y) unionn(id[i-1], id[i]);
}
for (int i = 0; i < n; ++i) fa[i] = find(i);
int tot = -1;
for (int i = 0; i < n; ++i) {
if (!vis[fa[i]]) vis[fa[i]] = true, f[++tot] = fa[i], m[fa[i]] = tot;
v[m[fa[i]]].push_back(i);
}
LL ans = 1;
for (int i = 0; i <= tot; ++i) {
sx.clear(), sy.clear();
for (auto idx : v[i]) {
sx.insert(a[idx].x), sy.insert(a[idx].y);
}
LL mul = poww(2, sx.size()+sy.size());
if (!circ[f[i]]) (mul += mod-1) %= mod;
(ans *= mul) %= mod;
}
printf("%I64d\n", ans);
return 0;
}
Codeforces 870E Points, Lines and Ready-made Titles 计数的更多相关文章
- Codeforces 870E Points, Lines and Ready-made Titles:并查集【两个属性二选一】
题目链接:http://codeforces.com/problemset/problem/870/E 题意: 给出平面坐标系上的n个点. 对于每个点,你可以画一条经过这个点的横线或竖线或什么都不画. ...
- codeforces 872E. Points, Lines and Ready-made Titles
http://codeforces.com/contest/872/problem/E E. Points, Lines and Ready-made Titles time limit per te ...
- Codeforces Round #440 (Div. 1, based on Technocup 2018 Elimination Round 2) C - Points, Lines and Ready-made Titles
C - Points, Lines and Ready-made Titles 把行列看成是图上的点, 一个点(x, y)就相当于x行 向 y列建立一条边, 我们能得出如果一个联通块是一棵树方案数是2 ...
- Codeforces 871C 872E Points, Lines and Ready-made Titles
题 OvO http://codeforces.com/contest/871/problem/C ( Codeforces Round #440 (Div. 1, based on Technocu ...
- 【题解】Points, Lines and Ready-made Titles Codeforces 871C 图论
Prelude 真是一道好题,然而比赛的时候花了太多时间在B题上,没时间想这个了QAQ. 题目链接:萌萌哒传送门(.^▽^) Solution 观察样例和样例解释,我们发现,假如有四个点,恰好占据在某 ...
- CodeForces 19D Points
Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a Cartesian coo ...
- R语言:多个因变量时,如何在plot函数中画多条曲线(plot,points,lines,legend函数)
最近阅读一篇文献<Regional and individual variations in the function of the human eccrine sweat gland>, ...
- CodeForces 19D Points (线段树+set)
D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- 『ACM C++』 Codeforces | 1066A - Points in Segments
大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席.团委团总支.社团社长都体验过一番了,现在差个班长也没试过,就来体验了 ...
随机推荐
- Catalan数,括号序列和栈
全是入门的一些东西.基本全是从别处抄的. 栈: 支持单端插入删除的线性容器. 也就是说,仅允许在其一端加入一个新元素或删除一个元素. 允许操作的一端也叫栈顶,不允许操作的一端也叫栈底. 数个箱子相叠就 ...
- 【JavaScript&jQuery】前端资源大全
综合类 综合类 地址 前端知识体系 http://www.cnblogs.com/sb19871023/p/3894452.html 前端知识结构 https://github.com/Jackson ...
- QoS专题-第1期-QoS理论篇
QoS理论篇 1 QoS的产生 随着网络技术的飞速发展,IP网络已经从当初的单一数据网络向集成数据.语音.视频.游戏的多业务网络转变.网络中所承载的数据呈几何级倍数增长,而且这些业务对网络带 ...
- Expect the Expected UVA - 11427(概率dp)
题意: 每天晚上你都玩纸牌,如果第一次就赢了,就高高兴兴的去睡觉,如果输了就继续玩.假如每盘游戏你获胜的概率都为p,每盘游戏输赢独立.如果当晚你获胜的局数的比例严格大于p时才停止,而且每天晚上最多只能 ...
- CF878C Tournament set 图论
题面 题面 题解 如果2个人可以互相战胜,那么我们连一条无向边,于是最后会剩下t个联通块,其中每对联通块之间都有严格的大小关系(a.max < b.min),因此我们每插入一个点就相当于合并一段 ...
- 【BZOJ3242】【NOI2013】快餐店(动态规划)
[BZOJ3242][NOI2013]快餐店(动态规划) 题面 BZOJ 题解 假设我们要做的是一棵树,那么答案显然是树的直径的一半. 证明? 假设树的直径是\(2d\),那么此时最远点的距离是\(d ...
- 【BZOJ4152】The Captain(最短路)
[BZOJ4152]The Captain(最短路) 题面 BZOJ Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求 ...
- opencv 获取摄像头图像
http://www.cnblogs.com/epirus/archive/2012/06/04/2535190.html #include "stdafx.h" #include ...
- python基础--文件操作实现全文或单行替换
python修改文件时,使用w模式会将原本的文件清空/覆盖.可以先用读(r)的方式打开,写到内存中,然后再用写(w)的方式打开. 替换文本中的taste 为 tasting Yesterday whe ...
- 曼-惠特尼U检验Mann–Whitney U Test
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...