BZOJ 4569: [Scoi2016]萌萌哒【倍增并查集

80 篇文章 0 订阅
2 篇文章 0 订阅

倍增并查集,考试naive地写的暴力_(:_ゝ∠)_

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;	int n,m;
const int MAXN=100057;
const int MOD=1000000007;
int f[17][MAXN];
int a,b,c,d,k;
long long ans=9;
int	findfather(int c,int x){ return f[c][x]==x?x:f[c][x]=findfather(c,f[c][x]);}
void merge(int c,int x,int y){
	if(findfather(c,x)==findfather(c,y))	return ;
	f[c][f[c][x]]=f[c][y];
	if(!c)	return ;
	--c;
	merge(c,x,y),merge(c,x+(1<<c),y+(1<<c));
} 

int main(){
	scanf("%d%d",&n,&m);
	if(n==1)	return puts("10"),0;
	for(int i=0;(1<<i)<=n;++i)	for(int j=1;j+(1<<i)-1<=n;++j)	f[i][j]=j;
	while(m--){
		scanf("%d%d%d%d",&a,&b,&c,&d);
		if(a==c)	continue;
		for(k=0;(1<<k)<=(b-a+1);++k);
		--k;
//		k=31-__builtin_clz(b-a+1);
		merge(k,a,c),merge(k,b-(1<<k)+1,d-(1<<k)+1);
	}
	int cnt=-1;
	for(int i=1;i<=n;++i)
		if(findfather(0,i)==i)	++cnt;
	
	while(cnt-->0){
		ans*=10;
		if(ans>=MOD)	ans%=MOD;
	}
	cout<<ans;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值