ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Xor-MST (最小异或生成树 贪心 01Trie) [2022.7.22]

2022-07-22 08:00:14  阅读:133  来源: 互联网

标签:rt 结点 ch Xor 22 01Trie int 复杂度 样例


Xor-MST

题面翻译

  • 给定 \(n\) 个结点的无向完全图。每个点有一个点权为 \(a_i\)。连接 \(i\) 号结点和 \(j\) 号结点的边的边权为 \(a_i\oplus a_j\)。
  • 求这个图的 MST 的权值。
  • \(1\le n\le 2\times 10^5\),\(0\le a_i< 2^{30}\)。

题目描述

You are given a complete undirected graph with $ n $ vertices. A number $ a_{i} $ is assigned to each vertex, and the weight of an edge between vertices $ i $ and $ j $ is equal to $ a_{i}xora_{j} $ .

Calculate the weight of the minimum spanning tree in this graph.

输入格式

The first line contains $ n $ ( $ 1<=n<=200000 $ ) — the number of vertices in the graph.

The second line contains $ n $ integers $ a_{1} $ , $ a_{2} $ , ..., $ a_{n} $ ( $ 0<=a_{i}<2^{30} $ ) — the numbers assigned to the vertices.

输出格式

Print one number — the weight of the minimum spanning tree in the graph.

样例 #1

样例输入 #1

5
1 2 3 4 5

样例输出 #1

8

样例 #2

样例输入 #2

4
1 2 3 4

样例输出 #2

8

解题思路

这题吧,最小异或生成树模板题。

由于数据规模过大,不能建出所有的边。

于是我们想到搞一棵01Trie,插入每隔点权,然后就将问题转化为在一个01Trie上把叶子结点联通的最小异或代价

那么对于一个树上结点,问题又可以分解为先把两个子树内部分别联通的子问题,然后再把两个子树联通

插入的复杂度 \(O(nlogm)\) ,其中m是值域, n是数的个数

令递归解决问题的复杂度为 \(T(n)\) ,每次把问题分为规模为原问题一半的两个子问题,

合并两子树则需遍历一棵子树内的所有叶子节点去另一棵字数里匹配出最小值, 这一步的复杂度为 \(O(nlogm)\)

所以有 \(T(n) = 2T(\dfrac{n}{2})\ +\ O(\dfrac{n}{2}logm)\)

接出来 \(T(n) = O(nlognlogm)\)

如果值域过大,还有更为优秀的时间复杂度为 \(O(nlog^2n)\) 的做法,但是我不会

code

#include<bits/stdc++.h>
using namespace std;

inline int read() {
	char ch = getchar();
	int res = 0, f = 1;
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = 0;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		res = (res << 1) + (res << 3) + (ch ^ 48);
		ch = getchar();
	}
	return f ? res : -res;
}

const int N = 2e5 + 5;
const int len = 30;

int n, a[N], rot = 1;
int tot = 1;
long long ans = 0;

int son[N * 40][2];
vector<int> st[N * 40];

void insert(int &rt, int val, int dep) {
	if (!rt) rt = ++tot;
	st[rt].push_back(val);
	if (dep == -1) return;
	insert(son[rt][bool(val & (1 << dep))], val, dep - 1);
}

int query(int rt, int val, int dep) {
	if (dep == -1) return 0;
	bool op = val & (1 << dep);
	return son[rt][op] ? 
			query(son[rt][op], val, dep - 1) : ( (1 << dep) + query(son[rt][op ^ 1], val, dep - 1) ); 
}

void work(int rt, int dep) {
	
	if (son[rt][0]) work(son[rt][0], dep - 1);
	if (son[rt][1]) work(son[rt][1], dep - 1);
	if (son[rt][0] && son[rt][1]) {
		int res = INT_MAX;
		for (int i : st[son[rt][0]]) {
			res = min(res, (1 << dep) + query(son[rt][1], i, dep - 1));
		}
		ans += res;
	}
}

int main() {
	n = read();
	for (int i = 1; i <= n; i++) a[i] = read();
	for (int i = 1; i <= n; i++) insert(rot, a[i], len);
	
	work(rot, len);
	
	printf("%lld", ans);
	
	return 0;
}

标签:rt,结点,ch,Xor,22,01Trie,int,复杂度,样例
来源: https://www.cnblogs.com/Aiza/p/16504289.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有