题目链接:http://codeforces.com/problemset/problem/870/E

题意:

  给出平面坐标系上的n个点。

  对于每个点,你可以画一条经过这个点的横线或竖线或什么都不画。

  两条重合的直线算作一条直线。

  问你能画出多少种不同的图案。

题解:

  将所有横坐标或纵坐标相同的两点之间连边。

  对于一个连通块,设这个连通块中不同的横坐标个数为sx,不同的纵坐标个数为sy。

  有可能画出的线的个数即为sx + sy。

  可以发现,如果一个联通块中有环(即siz[fa] >= sx + sy)

  那么这sx + sy条边可以同时画出。

  否则必然有一条边不能画出。

  所以当前连通块的答案:有环为2^(sx+sy),无环为2^(sx+sy) - 1。

  将所有连通块的答案乘起来即为总答案。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#define MAX_N 100005
#define MAX_E 200005
#define MOD 1000000007 using namespace std; struct Coor
{
int x,y,id;
Coor(int _x,int _y,int _id)
{
x=_x; y=_y; id=_id;
}
Coor(){}
}; int n;
int par[MAX_N];
int siz[MAX_N];
long long ans=;
long long pw[MAX_E];
Coor c[MAX_N];
set<int> sx[MAX_N];
set<int> sy[MAX_N]; bool cmp1(const Coor &a,const Coor &b)
{
return a.y!=b.y ? a.y<b.y : a.x<b.x;
} bool cmp2(const Coor &a,const Coor &b)
{
return a.x!=b.x ? a.x<b.x : a.y<b.y;
} void read()
{
cin>>n;
for(int i=;i<=n;i++)
{
cin>>c[i].x>>c[i].y;
c[i].id=i;
}
} void init_union_find()
{
for(int i=;i<=n;i++)
{
par[i]=i;
siz[i]=;
}
} int find(int x)
{
return par[x]==x ? x : par[x]=find(par[x]);
} void unite(int x,int y)
{
int px=find(x);
int py=find(y);
if(px==py) return;
siz[py]+=siz[px];
par[px]=py;
} void build()
{
sort(c+,c++n,cmp1);
for(int i=;i<n;i++)
{
if(c[i].y==c[i+].y)
{
unite(c[i].id,c[i+].id);
}
}
sort(c+,c++n,cmp2);
for(int i=;i<n;i++)
{
if(c[i].x==c[i+].x)
{
unite(c[i].id,c[i+].id);
}
}
} void cal_pow()
{
pw[]=;
for(int i=;i<MAX_E;i++) pw[i]=(pw[i-]<<1ll)%MOD;
} void cal_set()
{
for(int i=;i<=n;i++)
{
sx[find(c[i].id)].insert(c[i].x);
sy[find(c[i].id)].insert(c[i].y);
}
} inline long long mod(long long x)
{
return (x%MOD+MOD)%MOD;
} void cal_ans()
{
for(int i=;i<=n;i++)
{
int fa=find(i);
if(fa==i)
{
int edge=sx[fa].size()+sy[fa].size();
if(siz[fa]>=edge) ans=mod(ans*mod(pw[edge]));
else ans=mod(ans*mod(pw[edge]-));
}
}
} void work()
{
init_union_find();
build();
cal_pow();
cal_set();
cal_ans();
cout<<ans<<endl;
} int main()
{
read();
work();
}

Codeforces 870E Points, Lines and Ready-made Titles:并查集【两个属性二选一】的更多相关文章

  1. Codeforces 859E Desk Disorder:并查集【两个属性二选一】

    题目链接:http://codeforces.com/problemset/problem/859/E 题意: 有n个人,2n个座位. 给出这n个人初始的座位,和他们想坐的座位. 每个人要么坐在原来的 ...

  2. Codeforces 870E Points, Lines and Ready-made Titles 计数

    题目链接 题意 给定二维坐标上的\(n\)个点,过每个点可以 画一条水平线 或 画一条竖直线 或 什么都不画,并且若干条重合的直线被看做同一条.问共可能得到多少幅不同的画面? 题解 官方题解 仆の瞎扯 ...

  3. 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 ...

  4. Codeforces 1140F Extending Set of Points 线段树 + 按秩合并并查集 (看题解)

    Extending Set of Points 我们能发现, 如果把x轴y轴看成点, 那么答案就是在各个连通块里面的x轴的个数乘以y轴的个数之和. 然后就变成了一个并查集的问题, 但是这个题目里面有撤 ...

  5. Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集

    C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...

  6. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

  7. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  8. Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集

    D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...

  9. Codeforces 745C:Hongcow Builds A Nation(并查集)

    http://codeforces.com/problemset/problem/744/A 题意:在一个图里面有n个点m条边,还有k个点是受限制的,即不能从一个受限制的点走到另外一个受限制的点(有路 ...

随机推荐

  1. eclipse java文件提示 The import XXX cannot be resolved

    问题:eclipse导入类   提示The import XXX cannot be resolved 原因:原来使用JDK和现在使用的JDK不同造成的buildpath不对 解決方法: 1.右键项目 ...

  2. What are DESC and ASC Keywords?

    What are DESC and ASC Keywords? ASC is the short form for ascending DESC is the short form for desce ...

  3. SpringBoot处理url中的参数的注解

    1.介绍几种如何处理url中的参数的注解 @PathVaribale  获取url中的数据 @RequestParam  获取请求参数的值 @GetMapping  组合注解,是 @RequestMa ...

  4. 0x07 MySQL 多表查询

    Some Content From——Egon's Blog http://www.cnblogs.com/linhaifeng/articles/7126847.html 一 准备表 准备表 #建表 ...

  5. P2P-BT对端管理协议(附BT协议1.0)

    对端管理 指的是远端peer集合的管理(尽管自身client也能够视为一个peer.但对端管理不包括自身peer) 一个client(client)必须维持与每一个远程peer连接的状态信息,即1V1 ...

  6. spring boot 2.0添加对fastjson的支持

    首先引入fastjson的maven依赖: <dependency> <groupId>com.alibaba</groupId> <artifactId&g ...

  7. Shell Step by Step

    @1:Command: ctrl+z  ------->切后台 fg ------->切前台 Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + u :从光标 ...

  8. Shortcut Keys in Eclipse

    @1: Here are some shortcut keys in Eclipse that I use a lot.Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开 ...

  9. open函数and文件处理

    一 介绍 计算机系统分为:计算机硬件,操作系统,应用程序三部分 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,应用程序是无法操作 ...

  10. HDF 文件数据的读取

    http://www.cams.cma.gov.cn/cams_973/cheres_docs/cheres_doc_sat.modis.1b.html一. HDF文件格式 1.概述 HDF 是美国国 ...