Educational Codeforces Round 32 G. Xor-MST 01tire + 分治 + Boruvka

传送门

文章目录

题意:

给你一个长度为 n n n序列 a a a,每两个点之间的边权为 a i ⊕ a j a_i\oplus a_j aiaj,问你最小生成树的权值是多少。

n ≤ 2 e 5 , a i < 2 30 n\le2e5,a_i< 2^{30} n2e5,ai<230

思路:

看到最小生成树,我们可以想到克鲁斯卡尔算法,但是完全图显然不能直接做,但是异或的话显然我们需要放到 t i r e tire tire树上来跑的。

这里先介绍一种基于 t i r e tire tire树分治的算法。

根据克鲁斯卡尔的思想,我们需要找当前边权最小的点来合并,显然就是两个点的 l c a lca lca中深度最深的位置,但是这个比较难搞啊,既然我们都放在 t i r e tire tire树上了,为什么不利用一下 t i r e tire tire树的性质呢?

根据 t i r e tire tire树我们可知,每个点最多有两个儿子,并且可以拆位来看贡献!

所以我们遍历 t i r e tire tire树,找每个可能为 l c a lca lca的点,找一个最小的边,加上当前位的贡献即可。

一个小技巧,我们可以将 a a a排序后依次插入,那么 t i r e tire tire每个点的区间都是连续的。

复杂度 O ( n l o g 2 n ) O(nlog^2n) O(nlog2n)

在考虑利用 B o r u v k a Boruvka Boruvka算法来解决。

我们需要使用 B o r u v k a Boruvka Boruvka算法来快速找出对于每个连通块,与他连边最小的联通块是哪个,让后不断折半,最多进行 l o g n logn logn次。

考虑对每一个点 a i a_i ai建一颗字典树,让后对全局建一颗字典树,比如要查与 i i i这个连通块异或值最小的联通块,我们可以利用全局的 t i r e a l l tire_{all} tireall减去当前的 t i r e i tire_{i} tirei,这样就得到了其他连通块的 t i r e tire tire,在上面贪心的找即可,合并的时候启发式合并即可。

不知道为何 a a a排序就过了,不排序过不了。

复杂度 O ( n l o g 2 n ∗ α ) O(nlog^2n*\alpha) O(nlog2nα)

// Problem: G. Xor-MST
// Contest: Codeforces - Educational Codeforces Round 32
// URL: https://codeforces.com/contest/888/problem/G
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=200010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
LL a[N];
int tr[N*32][2],idx;
int l[N*32],r[N*32];

void insert(int x,int id) {
	int p=0;
	for(int i=30;i>=0;i--) {
		int u=x>>i&1;
		if(!tr[p][u]) tr[p][u]=++idx;
		if(!l[p]) l[p]=id; r[p]=id;
		p=tr[p][u];
	}
	if(!l[p]) l[p]=id; r[p]=id;
}

LL query(int x,int p,int dep) {
	if(dep==-1) return 0;
	int u=x>>dep&1;
	if(tr[p][u]) return query(x,tr[p][u],dep-1);
	else return query(x,tr[p][!u],dep-1)+(1<<dep);
}

LL dfs(int p,int dep) {
	if(dep==-1) return 0;
	if(tr[p][0]&&tr[p][1]) {
		LL ans=1e12;
		for(int i=l[tr[p][0]];i<=r[tr[p][0]];i++) {
			ans=min(ans,query(a[i],tr[p][1],dep-1));
		}
		return dfs(tr[p][0],dep-1)+dfs(tr[p][1],dep-1)+ans+(1<<dep);
	} else if(tr[p][1]) {
		return dfs(tr[p][1],dep-1);
	} else if(tr[p][0]) {
		return dfs(tr[p][0],dep-1);
	}
	return 0;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++) insert(a[i],i);
	printf("%lld\n",dfs(0,30));
	



	return 0;
}
/*

*/









// Problem: G. Xor-MST
// Contest: Codeforces - Educational Codeforces Round 32
// URL: https://codeforces.com/contest/888/problem/G
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL,LL> PII;

const int N=300010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
LL a[N];
int tr[N*50][2],root[N],idx,cnt[N*50],ed[N*50];
int p[N],se[N],id[N];
LL d[N];

int find(int x) {
	return x==p[x]? x:p[x]=find(p[x]);
}

void dfs(int p,int q) {
	cnt[p]+=cnt[q];
	ed[p]=ed[q];
	for(int i=0;i<2;i++) {
		if(tr[q][i]) {
			if(!tr[p][i]) {
				tr[p][i]=tr[q][i];
			} else dfs(tr[p][i],tr[q][i]);
		}
	}
}

void merge(int a,int b) {
	int pa=find(a),pb=find(b);
	if(pa==pb) return;
	if(se[pa]<se[pb]) dfs(root[pb],root[pa]),p[pa]=pb,se[pb]+=se[pa];
	else dfs(root[pa],root[pb]),p[pb]=pa,se[pa]+=se[pb];
}

void insert(int p,int x,int id) {
	ed[p]=id;
	for(int i=30;i>=0;i--) {
		int u=x>>i&1;
		if(!tr[p][u]) tr[p][u]=++idx;
		assert(idx<N*50);
		p=tr[p][u]; cnt[p]++;
		ed[p]=id;
	}
}

PII query(int x,int p) {
	int pp=root[0];
	LL ans=0;
	for(int i=30;i>=0;i--) {
		int u=x>>i&1;
		if(tr[pp][u]&&cnt[tr[pp][u]]-cnt[tr[p][u]]>0) p=tr[p][u],pp=tr[pp][u];
		else p=tr[p][!u],pp=tr[pp][!u],ans+=1<<i;
	}
	return {ed[pp],ans};
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	random_shuffle(a+1,a+1+n);
	root[0]=++idx;
	for(int i=1;i<=n;i++) {
		root[i]=++idx; p[i]=i; se[i]=1;assert(idx<N*50);
		insert(root[i],a[i],i); 
		insert(root[0],a[i],i);
	}
	// merge(3,2);  merge(1,3);
	// PII now=query(a[3],root[find(3)]);
	// cout<<now.X<<' '<<now.Y<<endl;
	// return 0;
	LL ans=0;
	int all=0;
	while(1) {
		for(int i=1;i<=n;i++) d[i]=(1ll<<31)-1;
		int cur=0;
		for(int i=1;i<=n;i++) {
			PII now=query(a[i],root[find(i)]);
			int x=find(i),y=find(now.X);
			if(x==y) continue;
			if(d[x]>now.Y) d[x]=now.Y,id[x]=now.X;
			if(d[y]>now.Y) d[y]=now.Y,id[y]=x;
		}
		for(int i=1;i<=n;i++) if(d[i]!=(1ll<<31)-1&&find(i)!=find(id[i])) {
			merge(i,id[i]); ans+=d[i];
			cur++;
		}
		if(!cur) break;
	}
	cout<<ans<<endl;



	return 0;
}
/*

*/









  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值