CodeForces - 888G Xor-MST(贪心+字典树+最小生成树)

该博客主要讨论了一种特殊的图问题——异或最小生成树。题目要求在给定的一组点中,根据异或运算连接任意两点的权值构建最小生成树。博主通过回顾去年的类似模型,介绍了如何利用字典树(Trie)数据结构来解决这个问题,强调了在处理过程中需要注意的答案可能会超出int范围。博主给出了详细的算法实现,并分享了完整的C++代码,包括字典树的插入和查询操作,以及如何通过递归求解最小生成树的异或和。

题目链接:点击查看

题目大意:给出 n n n 个点,任意两个点之间的边权为 a i ⊕ a j a_i\oplus a_j aiaj,求最小生成树

题目分析:去年多校写过一样的模型,再拿出来写一遍回顾一下:牛客多校5 - Graph

时间复杂度 O ( n l o g 2 n ) O(nlog^2n) O(nlog2n),一层 l o g log log 是字典树带着的,第二层 l o g log log 是需要在子树中查询异或值最小

有个小坑点是答案会爆 i n t int int,不要看到异或就太想当然了

代码:

// Problem: Xor-MST
// Contest: Virtual Judge - CodeForces
// URL: https://vjudge.net/problem/CodeForces-888G
// Memory Limit: 262 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{
	T f=1;x=0;
	char ch=getchar();
	while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	x*=f;
}
template<typename T>
inline void write(T x)
{
	if(x<0){x=~(x-1);putchar('-');}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=2e5+100;
int trie[N*30][2],tot;
vector<int>node[N*30];
int newnode() {
	tot++;
	trie[tot][0]=trie[tot][1]=0;
	return tot;
}
void insert(int x) {
	int pos=0;
	for(int i=29;i>=0;i--) {
		int to=x>>i&1;
		if(!trie[pos][to]) {
			trie[pos][to]=newnode();
		}
		pos=trie[pos][to];
	}
	if(node[pos].empty()) {
		node[pos].push_back(x);
	}
}
int search(int pos,int x,int dep) {
	int ans=0;
	for(int i=dep;i>=0;i--) {
		int to=x>>i&1;
		if(trie[pos][to]) {
			pos=trie[pos][to];
		} else {
			pos=trie[pos][!to];
			ans|=(1<<i);
		}
	}
	return ans;
}
LL solve(int rt,int dep) {
	LL ans=0;
	int ls=trie[rt][0],rs=trie[rt][1];
	if(ls) ans+=solve(ls,dep-1);
	if(rs) ans+=solve(rs,dep-1);
	if(ls&&rs) {
		int res=INT_MAX;
		for(auto it:node[ls]) {
			res=min(res,search(rs,it,dep-1));
		}
		ans+=res+(1<<dep);
	}
	node[rt].insert(node[rt].end(),node[ls].begin(),node[ls].end());
	node[rt].insert(node[rt].end(),node[rs].begin(),node[rs].end());
	return ans;
}
void init() {
	tot=-1;
	newnode();
}
int main()
{
#ifndef ONLINE_JUDGE
//	freopen("data.in.txt","r",stdin);
//	freopen("data.out.txt","w",stdout);
#endif
//	ios::sync_with_stdio(false);
	init();
	int n;
	scanf("%d",&n);
	for(int i=1,x;i<=n;i++) {
		read(x);
		insert(x);
	}
	cout<<solve(0,29)<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Frozen_Guardian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值