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.

在做这题的时候我们把边看成点 点看成边 那么一共 有两种情况 1 边是点数-1 那么可以构成的图的个数就是 2^点数-1 反之如果有环 可以构成的图就是2^点数 然后用组合数学乘一下就可以

并且可以知道如果不在一个联通块内肯定互不影响

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值