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个点是受限制的,即不能从一个受限制的点走到另外一个受限制的点(有路 ...
随机推荐
- <2013 07 29> 游泳
7月12日,在巴塞罗那的海滩学会用狗刨式游泳. 7月14日,在尼斯-戛纳海滩继续练习,稍式蛙泳仰泳. 7月28日,在慕尼黑某湖边吃烧烤,下湖练习. 7月29日,在慕尼黑奥林匹克游泳馆学会了仰泳,稍试自 ...
- MySQL复制(一):复制的基本步骤
从这里开始,对复制的内容开始做一些详细的描述,复制从简单到入门 复制最简单的模式如下 基本的三个简单步骤 1 配置一个服务器为master 2 配置一个服务器为slave 3 将slave连接到mas ...
- STL之内存处理工具
STL处理内存主要是使用五个全局函数construct,deconstruct,construct实现: template<typename T1,tyname T2> void cons ...
- springboot 常用的异常处理方式
springboot常用的异常处理推荐: 一.创建一个异常控制器,并实现ErrorController接口: package com.example.demo.controller; import o ...
- 曾经跳过的坑----jQuery mouseover与mouseenter,mouseout与mouseleave的区别
mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseou ...
- 请写一个python逻辑,计算一个文件中的大写字母数量
import os os.chdir(r'C:\Users\Administrator\Desktop')#os.chdir切换到指定目录 with open('a.txt') as today: c ...
- docker helloworld
阿里云镜像: docker官网 docker官方安装文档 配置阿里云镜像后,重启服务,以及检测服务是否正确启动 docker version.info.--help linux帮助命令man ls 镜 ...
- php大转盘抽奖
抽奖大转盘演示:http://www.sucaihuo.com/php/3301.html function getRand($proArr, $proCount) { $result = ''; $ ...
- jstl-functions标签
比如需要再jstl中定义一个String类型的数组 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl ...
- CentOS 5下freeswitch中集成使用ekho实现TTS功能二
三:以上Festival安装完成以后回到ekho安装目录: 执行./configure --enable-festival 前 更改configure 1:替换 #AC_DEFINE(ENABLE_F ...