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
大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席.团委团总支.社团社长都体验过一番了,现在差个班长也没试过,就来体验了 ...
随机推荐
- Java7 Fork-Join 框架:任务切分,并行处理
概要 现代的计算机已经向多CPU方向发展,即使是普通的PC,甚至现在的智能手机.多核处理器已被广泛应用.在未来,处理器的核心数将会发展的越来越多.虽然硬件上的多核CPU已经十分成熟,但是很多应用程序并 ...
- 实现全站 HTTPS ,为什么国内网站总是那么slow&&low呀!
1 https://konklone.com/post/switch-to-https-now-for-free# https://theintercept.com/2014/11/20/non-pr ...
- Contest 3
A:非常裸的dp. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstrin ...
- [BZOJ3223]文艺平衡树 无旋Treap
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Description 您需要写一种数据结构(可参考题目标题),来维护一个 ...
- mysqldump备份恢复
数据库的备份恢复是DBA的必修课,本文首先讲解mysqldump完整备份恢复的操作方法,演示数据库完整备份的基本过程.然后再模拟数据库遭遇灾难性破坏,配合bin-log日志实验数据库灾难性恢复的步骤. ...
- 解题:POI 2009 TAB
题面 这也算是个套路题(算吗)?发现换来换去每行每列数的组成是不变的,那么就把每行每列拎出来哈希一下,复杂度$O(Tn^2log$ $n)$有点卡时=.=. 然而正解似乎不需要哈希,就像这样↓ ;i& ...
- 洛谷P2002 消息扩散
题目背景 本场比赛第一题,给个简单的吧,这 100 分先拿着. 题目描述 有n个城市,中间有单向道路连接,消息会沿着道路扩散,现在给出n个城市及其之间的道路,问至少需要在几个城市发布消息才能让这所有n ...
- php数组定义
$arr = array( ,), //是’0' ,不是[‘0’] ,), ,), ,), ); $arr = [ '0' => array(2,3), //是’0' ,不是[‘0’] '1' ...
- duilib 修复padding属性导致其他控件自动计算宽高度错误的bug和导致自己宽高度错误的bug
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42950733 BUG 一:padding导致其他控件宽 ...
- Java常量池详解之Integer缓存
一个Java question,求输出结果 public class IntegerTest { public static void main(String[] args) { objPoolT ...