【Trie】MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016 Problem B. Be Friends
题意:一个n个点的完全图,点带权,边权是两端点点权的异或值。问你最小生成树。
一个性质,把所有点按照二进制最高位是否为1划分为2个集合,那么这两个集合间只会有一条边。可以递归处理。
把所有点建成01Trie,发现两个集合就是Trie的每个结点的两个子树。用启发式的思想,在小子树里dfs到叶子结点,取出每个值,然后去大子树里查询即可。
O(n(logn)^2)。
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int ch[100005*32][2],sz,siz[100005*32];
void Insert(int x)
{
int U=0;
for(int i=31-1;i>=0;--i){
if(!ch[U][(x>>i)&1]){
ch[U][(x>>i)&1]=++sz;
}
U=ch[U][(x>>i)&1];
}
}
int query(int x,int U,int nidep){
int res=0;
for(int i=nidep;i>=0;--i){
bool Bit=((x>>i)&1);
if(!ch[U][Bit]){
res|=(1<<i);
Bit^=1;
}
U=ch[U][Bit];
}
return res;
}
ll ans;
void dfsz(int U){
if(!ch[U][0] && !ch[U][1]){
siz[U]=1;
}
if(ch[U][0]){
dfsz(ch[U][0]);
siz[U]+=siz[ch[U][0]];
}
if(ch[U][1]){
dfsz(ch[U][1]);
siz[U]+=siz[ch[U][1]];
}
}
int nowans;
void df2(int U,int now,int rtnidep,int nidep,int otherrt){
if(!ch[U][0] && !ch[U][1]){
nowans=min(nowans,query(now,otherrt,rtnidep-1));
}
if(ch[U][0]){
df2(ch[U][0],now,rtnidep,nidep-1,otherrt);
}
if(ch[U][1]){
df2(ch[U][1],now|(1<<(nidep-1)),rtnidep,nidep-1,otherrt);
}
}
void dfs(int U,int nidep){
if(ch[U][0] && ch[U][1]){
nowans=2147483647;
if(siz[ch[U][0]]>siz[ch[U][1]]){
df2(ch[U][1],0,nidep,nidep,ch[U][0]);
}
else{
df2(ch[U][0],0,nidep,nidep,ch[U][1]);
}
nowans|=(1<<nidep);
ans+=nowans;
}
if(ch[U][0]){
dfs(ch[U][0],nidep-1);
}
if(ch[U][1]){
dfs(ch[U][1],nidep-1);
}
}
int n,m;
int main(){
// freopen("b.in","r",stdin);
int x;
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&x);
Insert(x);
}
dfsz(0);
dfs(0,30);
printf("%lld\n",ans);
return 0;
}
【Trie】MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016 Problem B. Be Friends的更多相关文章
- 【循环节】【矩阵乘法】MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016 Problem F. Fibonacci of Fibonacci
题意:F(n)为斐波那契数列的第n项,问你F(F(n)) mod 20160519的值. 发现有循环节,F(26880696)=0,F(26880697)=1,.... 于是两次矩乘快速幂即可. #i ...
- 【推导】【凸包】MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016 Problem D. Drawing Hell
平面上n个点,两个人交替决策,用线段连接两个点,但不能跨越其他点或者已经存在的线段.不能做的人算输,问你谁赢. 实际上,跟两个人的决策无关,n个点将平面三角剖分,只需要算出有几条边即可. 凸包上如果有 ...
- 【分块】MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016 Problem A. As Easy As Possible
给你一个字符串,多次区间询问,问你在该区间内最多能有几个easy重复的子序列. 显然如果只有一次询问,从左到右贪心做即可. 分块,预处理任意两块间的答案,不过要把以e a s y开头的四个答案都处理出 ...
- Problem I. Increasing or Decreasing MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016
题面: Problem I. Increasing or DecreasingInput file: standard inputOutput file: standard outputTime li ...
- 2016 MIPT Pre-Finals Workshop Taiwan NTU Contest
2016弱校联盟十一专场10.5 传送门 A. As Easy As Possible 假设固定左端点,那么每次都是贪心的匹配\(easy\)这个单词. 从\(l\)开始匹配的单词,将\(y\)的位置 ...
- 【Trie】背单词
参考博客: https://www.luogu.org/problemnew/solution/P3294 https://blog.csdn.net/VictoryCzt/article/detai ...
- 【Trie】L 语言
[题目链接]: https://loj.ac/problem/10053 [题意]: 给出n个模式串.请问文本串是由多少个模式串组成的. [题解]: 当我学完AC自动机后,发现这个题目也太简单了吧. ...
- 【Trie】Nikitosh 和异或
[参考博客]: LOJ#10051」「一本通 2.3 例 3」Nikitosh 和异或(Trie [题目链接]: https://loj.ac/problem/10051 [题意]: 找出两个不相交区 ...
- 【Trie】Phone List
[题目链接]: https://loj.ac/problem/10049 [题意] 问是否存在一组公共前缀.如果存在输出“NO”,否则输出“YES” [题解] 首先建出Trie树来,然后开始记录所有的 ...
随机推荐
- PAT L2-017. 人以群分
题目链接:https://www.patest.cn/contests/gplt/L2-017 题目: 社交网络中我们给每个人定义了一个“活跃度”,现希望根据这个指标把人群分为两大类,即外向型(out ...
- javascript继承机制 & call apply使用说明
一.继承机制 1.对象冒充:构造函数使用 this 关键字给所有属性和方法赋值,可使 ClassA 构造函数成为 ClassB 的方法,然后调用它. function ClassZ() { this. ...
- 网页实现插入图片—css与html的区别
Q1.二者有何区别?A1.写在css里面的图片是以背景图形式存在的,而写在html里的是以<img>标签形式存在的,在网页加载的过程中,以css背景图存在的图片会等到结构加载完成(网页的内 ...
- Join vs merge vs lookup
The obvious benefit of merge over join is the ability to add reject links. I can't upload pictures. ...
- 多表数据转化器MTDC
需求 根据配置文件的映射规则,将一种模型和数据映射成另外一种模型和数据.如图: 其中,a1,b1,c1,d1为表主键,关系:A.a1=B.b1=C.c2=D.d1 解决思路 解析模型配置文件,将每个转 ...
- 排名函数row_number() over(order by)用法
1. 定义 简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW_NUMBER() OVER (ORDER BY [列名]DESC) 是先把[列名]降序排列,再为降序以 ...
- HTML5API(3)
十一.ajax同源操作 URL说明是否允许通信 同一域名下允许 http://www.a.com/a.js , http://www.a.com/b.js 同一域名下不同文件夹允许 http://ww ...
- ASPxgridview 编辑列初始化事件
在初始化编辑咧的时候,给其赋值或者是disable等等.... 贴上代码 protected void master_CellEditorInitialize(object sender, ASPxG ...
- C++智能指针: auto_ptr, shared_ptr, unique_ptr, weak_ptr
本文参考C++智能指针简单剖析 内存泄露 我们知道一个对象(变量)的生命周期结束的时候, 会自动释放掉其占用的内存(例如局部变量在包含它的第一个括号结束的时候自动释放掉内存) int main () ...
- 3.22. grep sed re
1.整理正则表达式博客 re http://www.cnblogs.com/oyoui/p/6599846.html 2.grep(正则表达式及字符处理) 1.显示出所有含有root的行: [roo ...