codeforces 870E Points, Lines and Ready-made Titles

http://www.elijahqi.win/archives/1299
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
Note
In the first example there are two vertical and two horizontal lines passing through the points. You can get pictures with any subset of these lines. For example, you can get the picture containing all four lines in two ways (each segment represents a line containing it).

The first way: The second way:
In the second example you can work with two points independently. The number of pictures is 32 = 9.

#include<cstdio>
#include<algorithm>
#define N 110000
#define mod 1000000007
#define ll long long
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
return x*f;
}
int n,x[N],y[N],xx[N],yy[N],fa[N<<1],size[N<<1];bool flag[N],cir[N<<1];long long ans=1;
inline pow(int k){
long long ans1=1,base=2;
for (;k;k>>=1,(base*=base)%=mod) if (k&1) (ans1*=base)%=mod;
return ans1;
}
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main(){
freopen("cf.in","r",stdin);
n=read();
for (int i=1;i<=n;++i) x[i]=read(),y[i]=read(),xx[i]=x[i],yy[i]=y[i];
sort(xx+1,xx+1+n);sort(yy+1,yy+n+1);
int nx=unique(xx+1,xx+n+1)-xx-1;int ny=unique(yy+1,yy+n+1)-yy-1;
for (int i=1;i<=n;++i){
x[i]=lower_bound(xx+1,xx+nx+1,x[i])-xx;y[i]=lower_bound(yy+1,yy+ny+1,y[i])-yy;
}int nn=n<<1;
for (int i=1;i<=nn;++i) fa[i]=i,size[i]=1;
//将纵列的标号均加nx处理
//cir表示这个节点的联通块内是否有环
for (int i=1;i<=n;++i){
int fax=find(x[i]),fay=find(y[i]+nx);
if (fax==fay) cir[fax]=1; else{fa[fay]=fax;size[fax]+=size[fay];if (cir[fay]) cir[fax]=cir[fay];}
}
for (int i=1;i<=nx;++i){
int fa1=find(i);if (flag[fa1]) continue;flag[fa1]=1;
ll tmp=pow(size[fa1]);if (cir[fa1]) ans*=tmp;else ans*=tmp-1;ans%=mod;
}
printf("%d",ans);
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120