题意:一个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的更多相关文章

  1. 【循环节】【矩阵乘法】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 ...

  2. 【推导】【凸包】MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016 Problem D. Drawing Hell

    平面上n个点,两个人交替决策,用线段连接两个点,但不能跨越其他点或者已经存在的线段.不能做的人算输,问你谁赢. 实际上,跟两个人的决策无关,n个点将平面三角剖分,只需要算出有几条边即可. 凸包上如果有 ...

  3. 【分块】MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016 Problem A. As Easy As Possible

    给你一个字符串,多次区间询问,问你在该区间内最多能有几个easy重复的子序列. 显然如果只有一次询问,从左到右贪心做即可. 分块,预处理任意两块间的答案,不过要把以e a s y开头的四个答案都处理出 ...

  4. 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 ...

  5. 2016 MIPT Pre-Finals Workshop Taiwan NTU Contest

    2016弱校联盟十一专场10.5 传送门 A. As Easy As Possible 假设固定左端点,那么每次都是贪心的匹配\(easy\)这个单词. 从\(l\)开始匹配的单词,将\(y\)的位置 ...

  6. 【Trie】背单词

    参考博客: https://www.luogu.org/problemnew/solution/P3294 https://blog.csdn.net/VictoryCzt/article/detai ...

  7. 【Trie】L 语言

    [题目链接]: https://loj.ac/problem/10053 [题意]: 给出n个模式串.请问文本串是由多少个模式串组成的. [题解]: 当我学完AC自动机后,发现这个题目也太简单了吧. ...

  8. 【Trie】Nikitosh 和异或

    [参考博客]: LOJ#10051」「一本通 2.3 例 3」Nikitosh 和异或(Trie [题目链接]: https://loj.ac/problem/10051 [题意]: 找出两个不相交区 ...

  9. 【Trie】Phone List

    [题目链接]: https://loj.ac/problem/10049 [题意] 问是否存在一组公共前缀.如果存在输出“NO”,否则输出“YES” [题解] 首先建出Trie树来,然后开始记录所有的 ...

随机推荐

  1. 计蒜客 Goldbach Miller_Rabin判别法(大素数判别法)

    题目链接:https://nanti.jisuanke.com/t/25985 题目: Description: Goldbach's conjecture is one of the oldest ...

  2. HTML/CSS/JS编码规范

    最近整理了一份HTML/CSS/JS编码规范,供大家参考.目录:一.HTML编码规范二.CSS编码规范三.JS编码规范 一.HTML编码规范 1. img标签要写alt属性 根据W3C标准,img标签 ...

  3. RecycleView Bug:java.lang.IndexOutOfBoundsException: Inconsistency detected.

    今天使用RecyclerView时,上下两个RecyclerView,在实现下拉刷新时,报错: java.lang.IndexOutOfBoundsException: Inconsistency d ...

  4. CentOS7安装MySQL5.7以及修改密码

    CentOS7安装mysql [root@bd005 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch. ...

  5. 【Python学习笔记】使用Python进行主成分分析

    使用sklearn库中的PCA类进行主成分分析. 导入要用到的库,还没有的直接pip安装就好了. from sklearn.decomposition import PCA import numpy ...

  6. 微信小程序获取输入框(input)内容

    微信小程序---获取输入框(input)内容 wxml <input placeholder="请输入手机号码" maxlength="11" type= ...

  7. Android UI 设计:pixel dip dpi sp density

    1. px (pixels)像素 – 是像素,就是屏幕上实际的像素点单位. dip或dp (device independent pixels)设备独立像素,与设备屏幕有关. sp (scaled p ...

  8. HEER-Easing Embedding Learning by Comprehensive Transcription of Heterogeneous Information Networks

    来源:KDD 2018 原文:HEER code:https://github.com/GentleZhu/HEER 注: 若有错误,欢迎指正   这篇KDD’18的文章,没有按照常规的方法将所有的n ...

  9. Leetcode 之Binary Tree Preorder Traversal(42)

    树的先序遍历.定义一个栈,先压入中间结点并访问,然后依次压入右.左结点并访问. vector<int> preorderTraversal(TreeNode *root) { vector ...

  10. mybatis多表查询,自动生成id

    主要是在配置文件中,配置好所要包含的字段. 类关系:account----role,1对1 account包含role类 java类: public class Account{ private In ...