Codeforces 870E Points, Lines and Ready-made Titles:并查集【两个属性二选一】
题目链接: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:并查集【两个属性二选一】的更多相关文章
- Codeforces 859E Desk Disorder:并查集【两个属性二选一】
题目链接:http://codeforces.com/problemset/problem/859/E 题意: 有n个人,2n个座位. 给出这n个人初始的座位,和他们想坐的座位. 每个人要么坐在原来的 ...
- Codeforces 870E Points, Lines and Ready-made Titles 计数
题目链接 题意 给定二维坐标上的\(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 1140F Extending Set of Points 线段树 + 按秩合并并查集 (看题解)
Extending Set of Points 我们能发现, 如果把x轴y轴看成点, 那么答案就是在各个连通块里面的x轴的个数乘以y轴的个数之和. 然后就变成了一个并查集的问题, 但是这个题目里面有撤 ...
- 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 ...
- Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集
E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...
- 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, ...
- Codeforces 745C:Hongcow Builds A Nation(并查集)
http://codeforces.com/problemset/problem/744/A 题意:在一个图里面有n个点m条边,还有k个点是受限制的,即不能从一个受限制的点走到另外一个受限制的点(有路 ...
随机推荐
- Xamarin.Forms学习之Page Navigation(一)
在最初接触Xamarin.Forms的时候,我是跟着Xamarin官方的名为“learning-xamarin-ebook”的pdf文档进行学习的,我在成功运行Hello world程序之后,我开始跟 ...
- 移动端之touch事件--手指的滑动事件
转自[B5教程网]:http://www.bcty365.com/content-142-5243-1.html 总结:touchmove的最后坐标减去touchstart的起始坐标.X的结果如果正数 ...
- openvas-tutorial-for-beginners
https://jonathansblog.co.uk/openvas-tutorial-for-beginners
- <2013 06 29> In Deutschland. Thinking in Mechanism, EE, CS, etc.
一 在德国近一个月了,主要去了慕尼黑周边的几个景点,天鹅城堡啊,国王湖啊,然后就是在市区逛,玛丽安广场,伊萨河,英国公园,德意志博物馆... ... 总体的印象是非常好的,只是自己不怎么懂德语,但这里 ...
- python系列八:Python3条件控制&循环语句
#!/usr/bin/python #-*-coding:gbk-*-#Python3 条件控制&循环语句import randomage = int(input("请输入你的年龄: ...
- ehcache 的HelloWorld实现(二)
EhCache最新版本是3.X 本人一直用的是2.X 比较稳定 功能足够用: 所以还是用2.X版本: 我们新建一个Maven项目, pom.xml里引入 ehcache支持: <dependen ...
- PhpStorm2017.1版激活方法、汉化方法以及界面配置
本教程仅对2017.1版有效!!!!!! PhpStorm激活和汉化文件下载网址(提取密码:62cg) PhpStorm的介绍 PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提高用户效率, ...
- GIS学习和开发的在线资源
1.OpenGIS Consortium标准,http://www.opengeospatial.org.著名的OGC标准是每个GIS开发者最后都不得不学习的,或深或浅. 2.SharpMap,Pro ...
- RabbitMQ队列/Redis缓存
一.RabbitMQ队列 RabbitMQ安装(Centos7安装):1.安装依赖:yum install socat (不安装会报错)2.下载rpm包:wget http://www.rabbitm ...
- input propertychange(1)
input type=“text” 通过js改变输入框的value值是不会出发input propertychange事件