【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树来,然后开始记录所有的 ...
随机推荐
- RecycleView Bug:java.lang.IndexOutOfBoundsException: Inconsistency detected.
今天使用RecyclerView时,上下两个RecyclerView,在实现下拉刷新时,报错: java.lang.IndexOutOfBoundsException: Inconsistency d ...
- ctsc&apio2018八日游
day0: 早就知道自己是打酱油的..早就做好了打铁的准备.. Q:那你来干嘛 A:当然是来玩啊!!玩啊!啊!! emmmmm 抱着半期考不及格的卷子瑟瑟发抖地上了飞机. day1:报道!当然还有在宾 ...
- url编码模块
use LWP::SImple; use URI::Escape; encoded_string = uri_escape(raw_string); get(encoded_string);
- python近期遇到的一些面试问题(一)
整理一下最近被问到的一些高频率的面试问题.总结一下方便日后复习巩固用,同时希望可以帮助一些朋友们. 1.python的基本数据类型 主要核心类型分为两类不可变类型:数字(int float bool ...
- codevs 3287 货车运输 NOIP2013提高组
题目链接:http://codevs.cn/problem/3287/ 题解: 和bzoj3732一毛一样,只不过是找最大生成树和最小值罢了,具体参见我的bzoj3732的博客 #include< ...
- Lambda 表达式 in java 8
Lambda 表达式 in Java 8 Lambda表达式是java 8 新增的特性 Lambda表达式主要作用:支持将代码块作为方法参数,允许使用更简洁的代码创建函数式接口的实例,是匿名内部类的一 ...
- Python的数值和字符串
Python数据类型 1.数值 --类型: 1/整型 2/长整型 3/浮点型 -- 0.0, 12.0, -18.8, 3e+7等 4/复数型 -- complex In []: 0x34al ...
- Web开发中,页面渲染方案
转载自:http://www.jianshu.com/p/d1d29e97f6b8 (在该文章中看到一段感兴趣的文字,转载过来) 在Web开发中,有两种主流的页面渲染方案: 服务器端渲染,通过页面渲染 ...
- SQL中判断值是否为NULL
在 SQL 中,我们如果在操作数据库时使用 WHERE 子句判断一个列的值是否为 NULL,我们不能够使用 column_name=null 来进行判断,这是不正确的,我们应该使用 is null 来 ...
- redis使用教程
一.redis 的安装 官方就是个坑:只说make一下即可用,确实可以用,我以为装好了,结果好多问题: 安装步骤:make => make test => make install 1 ...