题目链接: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. 二维码及二维码接合短URL的应用

    二维码 1.什么是二维码? 二维条形码,最早发明于日本,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的“0 ...

  2. spring web中完成单元测试

    对于在springweb总完单元测试,之前找过些资料,摸索了很久,记录下最终自己使用的方法 1,创建测试类,创建测试资源文件夹 src/test/resources/WEB_INFO/conf 将工程 ...

  3. 修改 /var/lib/locales/supported.d/local 文件(使用 locale -a 命令查看系统中所有已配置的 locale)

    转自:http://zyxhome.org/wp/cc-prog-lang/c-stdlib-setlocale-usage-note/ http://www.west263.com/info/htm ...

  4. 随机生成六位验证码函数版(python)

    import random def code(n=6,alpha=True): s = '' # 创建字符串变量,存储生成的验证码 for i in range(n): # 通过for循环控制验证码位 ...

  5. samba了解

    1. samba是一个网络服务器,用于Linux和Windows之间共享文件 2,amba服务的启动.停止.重启    service smb start|stop|restart3. 掌握samba ...

  6. 003-ARP地址解析协议

    一.概念 地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/IP协议.主机发送信息时将包含目标IP地址的ARP请求广播到网络上的 ...

  7. C#编写图书列表winform

    Book.cs文件 using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...

  8. ACM解题之素矩阵

    题意: 如果一个矩形的两条边都是素数,则称此矩形为素矩形.本题给出一个素矩形的面积,请计算其两条边的值.有多个测试用例.每个用例占一行,包含一个表示素矩形面积且不超过 108 的正整数.输入直至没有数 ...

  9. 使用C# .NET 将结构数组绑定到 Windows 窗体的方法

      本任务的内容 概要 要求 设计结构 向数组添加结构实例 将结构成员绑定到窗体控件 提供浏览数组的方式 分步示例 参考 概要 本文介绍如何向 Windows 窗体绑定结构数组. 该示例由一个 Win ...

  10. public,protected,privat区别

    关于从基类继承来的方法和属性的保护: --class Pig:public Animal {...} C++不仅允许你对在类里定义的方法和属性实施访问控制,还允许你控制子类可以访问基类里的哪些方法和属 ...