codeforces 888G Xor-MST
You are given a complete undirected graph with n vertices. A number ai is assigned to each vertex, and the weight of an edge between vertices i and j is equal to ai xor aj.
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 a1, a2, ..., an (0 ≤ ai < 230) — the numbers assigned to the vertices.
Print one number — the weight of the minimum spanning tree in the graph.
本题要用到一种求生成树的方法Boruvka
给所有单词维护一颗trie树
按照这种求生成树的方法,我们每一次要在trie中去掉该集合的点,再求最小的边
这样很麻烦,实际上可以在trie树内部合并
首先左右子树中的所有点显然是已经在一个连通块内的。我们只需要在左右子树的联通块中各选出一
个点,连边即可
这样如果左子树存在右子树合并肯定最优,因为他们公共前缀最长
考虑启发式合并当前点的左右子树
枚举关键点数更小的子树的关键点,带入另一个子树求出最小边权
最后在根合并成一颗生成树
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long lol;
vector<lol>Q[];
int ch[][],pos,dep[],n;
lol pw[],ans,a[];
void insert(lol x)
{int i;
int now=;
Q[now].push_back(x);
for (i=;i>=;i--)
{
int flag=(bool)((x>>i)&);
if (ch[now][flag]==) ch[now][flag]=++pos;
now=ch[now][flag];
x-=flag*pw[i];
Q[now].push_back(x);dep[now]=i;
}
}
lol merge(int x,lol t)
{int i;
lol as=;
for (i=dep[x]-;i>=;i--)
{
int flag=(bool)((t>>i)&);
if (ch[x][flag]) x=ch[x][flag];
else as+=pw[i],x=ch[x][!flag];
}
return as;
}
lol query(int rt)
{int i;
if (Q[rt].size()==) return ;
if (ch[rt][]) ans+=query(ch[rt][]);
if (ch[rt][]) ans+=query(ch[rt][]);
if (!ch[rt][]||!ch[rt][]) return ;
int flag=;
if (Q[ch[rt][]].size()>Q[ch[rt][]].size()) flag=;
int sz=Q[ch[rt][flag]].size();
lol tmp=2e15;
for (i=;i<sz;i++)
tmp=min(tmp,merge(ch[rt][flag^],Q[ch[rt][flag]][i]));
return tmp+pw[dep[rt]-];
}
int main()
{int i;
cin>>n;
pw[]=;
for (i=;i<=;i++)
pw[i]=pw[i-]*;
for (i=;i<=n;i++)
{
scanf("%lld",&a[i]);
}
sort(a+,a+n+);
n=unique(a+,a+n+)-a-;
for (i=;i<=n;i++)
{
insert(a[i]);
}
ans+=query();
printf("%lld\n",ans);
}
首先左右子树中的所有点显然是已经在一个连通块内的。我们只需要在左右子树的联通块中各选出一
个点,连边即可
codeforces 888G Xor-MST的更多相关文章
- Codeforces.888G.Xor-MST(Borůvka算法求MST 贪心 Trie)
题目链接 \(Description\) 有一张\(n\)个点的完全图,每个点的权值为\(a_i\),两个点之间的边权为\(a_i\ xor\ a_j\).求该图的最小生成树. \(n\leq2*10 ...
- Codeforces 888G Xor-MST - 分治 - 贪心 - Trie
题目传送门 这是一条通往vjudge的高速公路 这是一条通往Codeforces的高速公路 题目大意 给定一个$n$阶完全图,每个点有一个权值$a_{i}$,边$(i, j)$的权值是$(a_{i}\ ...
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- Xor-MST Codeforces - 888G
https://codeforces.com/contest/888/problem/G 这题可以用Boruvka算法: 一开始每个点是一个连通块.每次迭代对于每个连通块找到其最近邻居(与其有边相连且 ...
- Codeforces 627A XOR Equation(思路)
题目大概说两个正整数a.b,已知s=a+b以及x=a xor b的值,问有几种a.b这样的数对. 我知道异或相当于无进位的加法,s-x就是其各个位置的进位,比如s-x=1010,那就表示a和b的第1位 ...
- CodeForces 242E - XOR on Segment 二维线段树?
今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...
- codeforces 242E. XOR on Segment 线段树
题目链接 给n个数, 两种操作, 一种是求区间内的数的和, 一种是将区间内的数异或x. 异或x没有什么思路, 单个异或肯定超时, 区间异或也没有办法做....后来才知道可以按位建线段树, 这样建20棵 ...
- Codeforces 888G(分治+trie)
按位贪心,以当前考虑位是0还是1将数分成两部分,则MST中这两部分之间只会存在一条边,因为一旦有两条或以上的边,考虑两条边在原图中所成的环,显然这两条边有一条是环上的权值最大边,不会出现在MST中.则 ...
- codeforces 242E - XOR on Segment (线段树 按位数建树)
E. XOR on Segment time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- Flask 文件和流
当我们要往客户端发送大量的数据比较好的方式是使用流,通过流的方式来将响应内容发送给客户端,实现文件的上传功能,以及如何获取上传后的文件. 响应流的生成 Flask响应流的实现原理就是通过Python的 ...
- 【iOS】swift-获取webView的高度
func webViewDidFinishLoad(webView: UIWebView) { let webHeightStr = webView.stringByEvalu ...
- java语法基础(总结)
1,关键字:其实就是某种语言赋予了特殊含义的单词. 保留字:其实就是还没有赋予特殊含义,但是准备日后要使用过的单词. 2,标示符:其实就是在程序中自定义的名词.比如类名,变量名,函数名.包含 0-9. ...
- 《网络》:设置三个密码:通过console口连接设备,进入特权模式,登录Telnet
软件:Cisco Packet Tracer Instructor 软件下载链接在上一篇文章中. 内容:通过设置三个密码,熟悉采用Telnet方式配置交换机的方法. 细节说明:计算机的IP地址和交换机 ...
- jQuery兼容浏览器IE8方法
在维护公司网站的时候,发现在IE8下jquery会报对象不支持此属性或方法.缺少对象的错误: 在其他浏览器就可以正常运行,当前使用的jquery版本是3.1.1,查资料发现jquery从2.0开始不 ...
- Python内置函数(7)——sum
英文文档: sum(iterable[, start]) Sums start and the items of an iterable from left to right and returns ...
- 新概念英语(1-5)Nice to meet you.
Is Chang-woo Chinese? Blake:Good morning. B:Good morning, Mr Blake. Blake:This is Miss Sophie Dupont ...
- C++ 排列最优解算法思想
枚举全排列 #include <iostream> #include <cstring> #include <string> using namespace std ...
- 前端插件之Bootstrap Switch 选择框开关控制
简介 Bootstrap Switch是一款轻量级插件,可以给选择框设置类似于开关的样式 它是依赖于Bootstrap的一款插件 下载 下载地址 在线引用 导入 因为它是依赖于Bootstrap的一款 ...
- logback中配置的日志文件的生成地址
配置文件如下 <?xml version="1.0" encoding="UTF-8"?> <configuration debug=&quo ...