[CF888G]Xor-MST
题目大意:给一个$n$个点的完全图,第$i$个点有点权$v_i$,一条边$x-y$的边权为$v_x\oplus v_y$,求最小生成树
题解:明显$Kruskal$和$Prim$都会$TLE$,有一种别的生成树的算法为$Sollin$。它对棵树找到离它最近的不连通的一棵树,对每棵树找好后若可以连这一条边就连接这条边。可以证明每次连通块个数至少减少一半,每次找最近的树可以枚举每一条边,复杂度$O(m)$,所以总复杂度是$O(m\log_2 n)$的。
在这一题中,可以用$Trie$来优化找最近的树的过程,可以优化为$O(\log_2 v)$,可以通过本题
卡点:发现两个点点权相同就不会产生贡献,于是就可以排序去重,记录最开始连通块个数时记录的是没有去重的点数,于是就一直连不完,一直$TLE$
C++ Code:
#include <algorithm>
#include <cctype>
#include <cstdio>
namespace __IO {
namespace R {
int x, ch;
inline int read() {
ch = getchar();
while (isspace(ch)) ch = getchar();
for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
return x;
}
}
}
using __IO::R::read; #define maxn 200010
const int inf = 0x3f3f3f3f; namespace Trie {
#define M 29
#define N (maxn * (M + 2))
int V[N], nxt[2][N], root, idx; void modify(int x, int num = 1) {
int p = root;
for (register int i = M; ~i; i--) {
int tmp = x >> i & 1;
if (!nxt[tmp][p]) nxt[tmp][p] = ++idx;
p = nxt[tmp][p];
V[p] += num;
}
} int query(int x) {
int p = root, res = 0;
for (register int i = M; ~i; i--) {
int tmp = x >> i & 1;
if (V[nxt[tmp][p]]) p = nxt[tmp][p];
else p = nxt[!tmp][p], res |= 1 << i;
}
return res;
}
#undef N
#undef M
} int n, num;
std::pair<int, int> M[maxn];
int s[maxn], rnk[maxn];
long long ans; int f[maxn];
int find(int x) {return x == f[x] ? x : (f[x] = find(f[x]));} inline bool cmp(int a, int b) {return f[a] < f[b];}
int main() {
n = read();
for (int i = 1; i <= n; i++) s[i] = read();
n = (std::sort(s + 1, s + n + 1), std::unique(s + 1, s + n + 1) - s - 1);
for (int i = 1; i <= n; i++) {
rnk[i] = f[i] = i;
Trie::modify(s[i]);
}
num = n - 1;
while (num) {
for (int i = 1; i <= n; i++) find(i);
std::sort(rnk + 1, rnk + n + 1, cmp);
for (int i = 1; i <= n; i++) M[i] = std::make_pair(i, inf);
for (int l = 1, r, father; l <= n; l = r + 1) {
father = f[rnk[r = l]];
while (r < n && father == f[rnk[r + 1]]) r++;
for (int i = l; i <= r; i++) Trie::modify(s[rnk[i]], -1);
for (int i = l; i <= r; i++) {
int val = Trie::query(s[rnk[i]]), pos = std::lower_bound(s + 1, s + n + 1, s[rnk[i]] ^ val) - s;
if (val < M[father].second) M[father] = std::make_pair(pos, val);
}
for (int i = l; i <= r; i++) Trie::modify(s[rnk[i]]);
}
for (int i = 1; i <= n; i++) {
int x = find(i), y = find(M[i].first);
if (x != y) {
f[x] = y;
ans += M[i].second;
num--;
}
}
}
printf("%lld\n", ans);
return 0;
}
[CF888G]Xor-MST的更多相关文章
- CF888G Xor-MST 解题报告
CF888G Xor-MST 题意翻译 给定一个\(n\)个节点的完全图,每个节点有个编号\(a_i\),节点\(i\)和节点\(j\)之间边的权值为\(a_i\ xor\ a_j\),求该图的最小生 ...
- hdu-5683 zxa and xor (位运算)
题目链接: zxa and xor Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- Codeforces.888G.Xor-MST(Borůvka算法求MST 贪心 Trie)
题目链接 \(Description\) 有一张\(n\)个点的完全图,每个点的权值为\(a_i\),两个点之间的边权为\(a_i\ xor\ a_j\).求该图的最小生成树. \(n\leq2*10 ...
- CF888G Xor-MST[最小生成树+01trie]
前注:关于这题,本人的解法暂时没有成功通过此题,原因是被卡常了.可能需要等待某种机缘来请人调试. 类似uoj的一道题(新年的繁荣),不过是一个有些简单的版本. 因为是完全图,有没有办法明显优化建边,所 ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- 二分+DP+Trie HDOJ 5715 XOR 游戏
题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- BZOJ 2115 【Wc2011】 Xor
Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...
- 基于MST的立体匹配及相关改进(A Non-Local Cost Aggregation Method for Stereo Matching)
怀着很纠结的心情来总结这篇论文,这主要是因为作者提虽然供了源代码,但是我并没有仔细去深究他的code,只是把他的算法加进了自己的项目.希望以后有时间能把MST这一结构自己编程实现!! 论文题目是基于非 ...
- xor和gates的专杀脚本
前段时间的一次样本,需要给出专杀,应急中遇到的是linux中比较常见的两个家族gates和xor. 首先是xor的专杀脚本,xor样本查杀的时候需要注意的是样本的主进程和子进程相互保护(详见之前的xo ...
随机推荐
- Express 总结
Express Express提供了一个轻量级模块,把nodejs的http功能封装在一个简单易用的接口中.Express也扩展了http模块的功能,能轻松处理服务器的路由.响应.cookie和HTT ...
- 从golang的垃圾回收说起(上篇)
本文来自网易云社区 1 垃圾回收中的重要概念 1.1 定义 In computer science, garbage collection (GC) is a form of automatic me ...
- word record 4
word record 4 pledge p le g vt. 保证,许诺 snowflake falke->n. 小薄片:火花 deputy de piu ti n. 代理人,代表 etch ...
- 微信小程序入门学习之事件 事件对象 冒泡非冒泡事件(1)
这关于事件的学习,可以自己复制到微信开发者工具上自己运行试试. 首先这里有两个文件.js 和.wxml 文件 首先给出.js文件下代码 // pages/news/news.js Page({ /** ...
- lintcode671 循环单词
循环单词 The words are same rotate words if rotate the word to the right by loop, and get another. Cou ...
- 【springmvc+mybatis项目实战】杰信商贸-2.数据库配置
首先我们来了解项目的架构 我们分别使用了MySql和Oracle数据库,即是异构数据库.我们做到一个平台支持多个数据库.数据库建模我们使用Sybase公司的PowerDesigner(以后简称PD), ...
- Shell 常用命令、基本用法总结
Filter Filter 常用于从大量文本.数据中提取需求的部分.下面介绍几个常用的 filter 命令. cut $ cut -c 5-8 textfile.txt # 切出 textfile.t ...
- ArrayList与LinkedList的普通for循环遍历
对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: public static void main(String[] ...
- vscode开发智能合约
开发工具 EOS 开发终极神器-vscode (你绝对找不到的干货) lome · 2018年04月19日 · 最后由 18636292520 回复于 2018年09月15日 · 15672 次阅读 ...
- wamp下安装https 实现 ssl 协议,主要是编写小程序通讯
也不知道腾讯为啥要这个限制,是想卖他的服务器资源么 简单几句话 1 wamp3.0.X的版本不行,我折腾了一天半,放弃了,换成wamp2.5 一次通过 2 证书 去腾讯云申请,单独域名的可以申请免费的 ...