题目链接: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. javascript基础(整理自手册网)

    变量 person=null; //清空变量 carname="Volvo"; //赋值给未声明的变量, 它将会变成全局变量, 即使在函数内部 window.carnam; //所 ...

  2. docker desktop

    https://github.com/rogaha/docker-desktop http://blog.csdn.net/tinylab/article/details/45443563

  3. error C1853: “Debug\BigBuffer.pch”预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)

    <pre id="best-content-1299104064" mb-10"="" style="font-size: 14px; ...

  4. 使用selenium进行密码破解(绕过账号密码JS加密)

    经常碰到网站,账号密码通过js加密后进行提交.通过burp拦截抓到的账号密码是加密后的,所以无法通过burp instruder进行破解.只能模拟浏览器填写表单并点击登录按钮进行破解.于是想到了自动化 ...

  5. Python3.6全栈开发实例[006]

    6.检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者. dic = {"k1": "v1v1", " ...

  6. python面试题(六)

    1 异常处理写法以及如何主动跑出异常(应用场景) try: """执行语句""" except: #异常类型 ""&qu ...

  7. JS单例

    s = (function S(){ var bean; function get(){ if(bean){ return bean }else{ bean = T(); return bean; } ...

  8. 使用git工具上传项目到github步骤

    这里记录一下上传项目到github的步骤.使用的工具是Git bash. 1.登陆github,没有账户就注册一个,新建一个Repository(仓库). 2.绑定用户. 因为Git是分布式版本控制系 ...

  9. Get Region Information from IP Address with Taobao API

    通过淘宝的API "http://ip.taobao.com/service/getIpInfo.php?ip=*.*.*.*" 来获得你要查询的IP地址的国家,地区,城市,ISP ...

  10. 一个用 C# 实现操作 XML 文件的公共类代码

    using System; using System.IO; using System.Data; using System.Xml; using System.Xml.XPath; namespac ...