codeforces 870E Points, Lines and Ready-made Titles

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
Copy
4
1 1
1 2
2 1
2 2
output
16
input
Copy
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个的点;  如果一个联通块有一个环 那么就能满足所有的点。这和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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值