Codeforces 870E 并查集 解题报告
来源:互联网 发布:诺亚网络萝卜街 编辑:程序博客网 时间:2024/05/16 13:46
Points, Lines and Ready-made Titles
You are given n distinct points on a plane with integral coordinates. For each point you can either draw a vertical line through it, draw a horizontal line through it, or do nothing.
You consider several coinciding straight lines as a single one. How many distinct pictures you can get? Print the answer modulo 109 + 7.
Input
The first line contains single integer n (1 ≤ n ≤ 105) — the number of points.
n lines follow. The (i + 1)-th of these lines contains two integers xi, yi ( - 109 ≤ xi, yi ≤ 109) — coordinates of the i-th point.
It is guaranteed that all points are distinct.
Output
Print the number of possible distinct pictures modulo 109 + 7.
Examples
input
4
1 1
1 2
2 1
2 2
output
16
input
2
-1 -1
0 1
output
9
【解题报告】
代码如下:
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;#define mod 1000000007#define N 100010int n,f[N],x[N],y[N],t[N<<1];struct Point{int x,y,id;}w[N];bool cmp1(Point a,Point b){return a.x<b.x;}bool cmp2(Point a,Point b){return a.y<b.y;}int find(int i){ return f[i]==i?i:f[i]=find(f[i]);}void unit(int i,int j){ i=find(i); j=find(j); if(i==j) y[i]++; else { f[j]=i; x[i]+=x[j]; y[i]+=y[j]+1; }}int main(){ scanf("%d",&n); t[0]=1; for(int i=1;i<=(n<<1);i++) t[i]=t[i-1]*2%mod; for(int i=1;i<=n;i++) { scanf("%d%d",&w[i].x,&w[i].y); w[i].id=i;f[i]=i;x[i]=1; } sort(w+1,w+n+1,cmp1); for(int i=2;i<=n;i++) if(w[i].x==w[i-1].x) unit(w[i].id,w[i-1].id); sort(w+1,w+n+1,cmp2); for(int i=2;i<=n;i++) if(w[i].y==w[i-1].y) unit(w[i].id,w[i-1].id); int ans=1; for(int i=1;i<=n;i++) { if(find(i)==i) { if(y[i]==x[i]-1) ans=(long long)ans*(t[x[i]+1]-1)%mod; else ans=(long long)ans*t[x[i]*2-y[i]]%mod; } } printf("%d\n",ans); return 0;}
- Codeforces 870E 并查集 解题报告
- 并查集解题报告
- 食物链 并查集 解题报告
- HDU 1232 - 并查集 解题报告
- Friend解题报告 - 并查集练习
- 并查集专题训练解题报告
- BZOJ 2054 并查集 解题报告
- BZOJ 3319 并查集 解题报告
- POJ 1182 并查集 解题报告
- HDU1213并查集解题报告
- Codeforces Goodbye 2014 problem B ACM解题报告(经典的并查集)
- codeforces Round #237(div2) E解题报告
- codeforces Round #241(div2) E解题报告
- codeforces Round #264(div2) E解题报告
- codeforces Round #263(div2) E解题报告
- codeforces Round #274(div2) E解题报告
- codeforces Round #261(div2) E解题报告
- codeforces Round #260(div2) E解题报告
- linux内核编译过程的最终总结版
- NKOJ 4250 小区划分(dp)
- python中常见运算符优先级使用规律
- 关于函数的返回
- 数据库学习纪要(二十七):MySQL-7-外联接、自联接与联合
- Codeforces 870E 并查集 解题报告
- idea for mac 最全快捷键整理
- CentOS7和CentOS6怎样开启MySQL远程访问
- Java8 I/O源码-BufferedInputStream与BufferedOutputStream
- cocos2dx JNI 中 C++层与Java层之间的交互
- http请求头(响应头)、http2、https
- Codeforces Round #440
- 面向对象技术---学习总结
- [NOI2015]荷马史诗 哈夫曼树