题意:一个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. Fermat2018游记

    day (-22) 2018年2月5日 Devin大佬给我发了一份Waterloo AIF的原件截图,发现里面居然直接问你的Fermat分数,那么这么重要的考试当然不能错过啊 若干天之后刚开学不久的一 ...

  2. 手動設定 電池溫度 mtk platform

    adb root adb shell echo "3 1 27" > ./proc/mtk_battery_cmd/battery_cmd 27 即是所要設定的溫度, 此設定 ...

  3. C基础 算法实现层面套路

    引言 - 从实践狗讲起 理论到实践(有了算法到实现) 中间有很多过程. 算法方面本人啥也不懂, 只能说说实现方面. 例如下面 一个普通的插入排序. // // 插入排序默认从大到小 // extern ...

  4. service XXX start启动报start: Rejected send message, 1 matche

    转,原文地址:http://blog.sina.com.cn/s/blog_56d8ea9001018w1l.html [问题]start: Rejected send messag现象:crifan ...

  5. python_基于反射模拟Web框架路由系统

    根据用户输入的内容,导入模块 #根据用户输入的内容,导入模块 inp = input("请输入模块名: ") print(inp,type(inp)) dd = __import_ ...

  6. 手机端调试console.log,直接引入一个js文件

    http://files.cnblogs.com/files/lwwen/mConsole.js 这是我写的一个原生js文件 直接引入即可,可以把html上面的需要打印的东西打印出来 <!DOC ...

  7. [编程题] 合唱团 && 地闹逃脱

    1. 合唱团 有 n 个学生站成一排,每个学生有一个能力值,牛牛想从这 n 个学生中按照顺序选取 k 名学生,要求相邻两个学生的位置编号的差不超过 d,使得这 k 个学生的能力值的乘积最大,你能返回最 ...

  8. hdu 1041(递推,大数)

    Computer Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/ ...

  9. Mayavi入门

    环境,win7/10 64位, python3.x 1,安装 Mayavi4.6 原装的pip下载奇慢,先更换一下源,豆瓣的更新要比清华的快 首先在window的文件夹窗口输入 : %APPDATA% ...

  10. es6 map数据类型,要比set还很多

    首先它支持多数据存储,具有增删查功能 set()设置 get()获取; has()查找; delete('obj')删除指定:clear()全部删除 size长度 let json={ name:&q ...