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 test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 xiyi ( - 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.

 

 題意:

給出二維平面上的n個點,每個點可以畫一條水平線,也可以畫一條豎直線,也可以什么都不畫

求 圖案 方案數

 

思路:把沖突的點放到一個連通塊中,對每個連通塊單獨處理,乘法原理計數

 

對於一個連通塊來說,n個點最多有n+1條邊

最開始一個點有2條邊,然后每加入一條邊,都要加入一個點

當然,可以只加點不加邊(例:井字形)

所以 邊數E<=點數P+1

 

因為連通塊里加入的這些邊,保證不沖突

所以

 

1、E==P+1

因為一個點只能連一條邊,所以這個連通塊最多只能有P條邊

所以這個連通塊的方案數=C(E,1)+C(E,2)+……+ C(E,P)= 2^E-1 

2、E<=P

方案數=C(E,1)+C(E,2)+……+C(E,E)= 2^E

 

#include<cstdio>
#include<iostream>
#include<algorithm>

#define N 100001

using namespace std;

const int mod=1e9+7;

int hasx[N],hasy[N],x[N],y[N];

int fa[N*2];

int sizp[N],size[N*2];

void read(int &x)
{
    x=0; int f=1; char c=getchar();
    while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }
    while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
    x*=f;
}

int find(int i) { return fa[i]==i ? i : fa[i]=find(fa[i]); }

int Pow(int a,int b)
{
    int res=1;
    for(;b;a=1ll*a*a%mod,b>>=1)
        if(b&1) res=1ll*res*a%mod;
    return res;
}

int main()
{
    int n; read(n);
    for(int i=1;i<=n;i++) read(x[i]),read(y[i]),hasx[i]=x[i],hasy[i]=y[i];
    sort(hasx+1,hasx+n+1); sort(hasy+1,hasy+n+1);
    int tot1=unique(hasx+1,hasx+n+1)-hasx-1,cnt=tot1;
    for(int i=1;i<=n;i++)  x[i]=lower_bound(hasx+1,hasx+tot1+1,x[i])-hasx;
    int tot2=unique(hasy+1,hasy+n+1)-hasy-1; cnt+=tot2;
    for(int i=1;i<=n;i++)  y[i]=lower_bound(hasy+1,hasy+tot2+1,y[i])-hasy;
    for(int i=1;i<=cnt;i++)  fa[i]=i;
    for(int i=1;i<=n;i++)  fa[find(x[i])]=find(y[i]+tot1);
    for(int i=1;i<=n;i++)  sizp[find(x[i])]++;
    for(int i=1;i<=cnt;i++) size[find(i)]++;
    int ans=1;
    for(int i=1;i<=cnt;i++)
        if(find(i)==i)
        {
            if(sizp[i]+1==size[i]) ans=1ll*ans*(Pow(2,size[i])+mod-1)%mod;
            else ans=1ll*ans*Pow(2,size[i])%mod;
        }
    printf("%d",ans);
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM