CF888G XOR-MST 最小异或生成树

CF888G XOR-MST

链接

CF888G

思路

trie上贪心,先左右两边连边,再用一条边的代价连起左右两颗树。因为内部的边一定比跨两棵树的边权笑,显然是对的。
代码自己瞎yy的。启发式合并

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int _=2e5+7;
int n,a[_],ch[_*30][2],siz[_*30],cnt,col,rt[_*30];
ll ans;
std::vector<int> dsr[_*30];
void insert(int x) {
    int p=0;
    for(int i=29;i>=0;--i) {
        bool k=x&(1LL<<i);
        siz[p]++;
        if(!ch[p][k]) ch[p][k]=++cnt;
        p=ch[p][k];
    } siz[p]++;
    if(!rt[p]) rt[p]=++col;
    dsr[rt[p]].push_back(x);
}
int query(int rt,int init,int x) {
    int p=rt,ans=0;
    for(int i=init;i>=0;--i) {
        bool k=x&(1LL<<i);
        if(ch[p][k]) p=ch[p][k];
        else p=ch[p][!k],ans|=1<<i;
    }
    return ans;
}
void dfs(int p,int dep) {
    if(!ch[p][0]&&!ch[p][1]) return;
    if(!ch[p][0]||!ch[p][1]) {
        dfs(ch[p][0]|ch[p][1],dep-1);
        rt[p]=rt[ch[p][0]|ch[p][1]];
    } else {
        int k=siz[ch[p][0]]<siz[ch[p][1]];
        dfs(ch[p][0],dep-1),dfs(ch[p][1],dep-1);
        rt[p]=rt[ch[p][k]];
        int tmp=0x3f3f3f3f;
        int nb=ch[p][!k];
        ch[p][!k]=0;
        for(auto x:dsr[rt[nb]]) {
            tmp=min(tmp,query(p,dep,x));
            dsr[rt[p]].push_back(x);
        }
        ans+=tmp;
        ch[p][!k]=nb;
    }
}
int main() {
    // freopen("a.in","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;++i) scanf("%d",&a[i]),insert(a[i]);
    dfs(0,29);
    cout<<ans<<"\n";
    return 0;
}

转载于:https://www.cnblogs.com/dsrdsr/p/11405990.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值