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.
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.
Print the number of possible distinct pictures modulo 109 + 7.
4 1 1 1 2 2 1 2 2
16
2 -1 -1 0 1
9
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).
In the second example you can work with two points independently. The number of pictures is 32 = 9.
题意:给你n个点的坐标,每个点可以画一条横线,或是竖线,或是什么也不做,求可能出现的图案数。
做法:这个题首先我们可以发现,一些横坐标相等的所有点是等价的,纵坐标同理。所以可以使用并查集合并所有等价的点,之后,再算每个联通块对于答案的贡献。
如果一个联通块形成了一棵n个点的树 那么只能满足其中的n-1个的点; 如果一个联通块有一个环 那么就能满足所有的点。这和bzoj1854连续攻击游戏同理。
之后我们用并查集点和边的套路统计法统计点和边的个数,然后直接算贡献,乘法原理。
代码:
#include <iostream>
#include <cstdio>
#include <map>
#define ll long long
using namespace std;
int n,tot,x[100005],y[100005],P[200005],E[200005],fa[200005];
ll ans=1;
map<int,int>xx,yy;
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
ll pow(ll di,ll mi)
{
ll res=1,a=di;
while(mi)
{
if(mi&1) res=res*a%1000000007;
a=a*a%1000000007;
mi>>=1;
}
return res;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);
if(!xx.count(x[i])) xx[x[i]]=++tot;
if(!yy.count(y[i])) yy[y[i]]=++tot;
}
for(int i=1;i<=tot;i++) fa[i]=i;
for(int i=1;i<=n;i++) fa[find(xx[x[i]])]=fa[find(yy[y[i]])];
for(int i=1;i<=n;i++) P[find(xx[x[i]])]++;
for(int i=1;i<=tot;i++) E[find(i)]++;
for(int i=1;i<=tot;i++)
if(i==find(i))
{
if(P[i]==E[i]-1) ans=(ans*(pow(2,E[i])-1))%1000000007;
else ans=(ans*pow(2,E[i]))%1000000007;
}
printf("%I64d\n",ans);
}