Codeforces 888G

【CF888G】 Xor-MST 简易题解_周道-Althen的博客-CSDN博客

这个题解真的很好

思路其实很简单,就是要想到怎么实现,并且不怕麻烦,才能写出这题出来

在这里插入图片描述

 上图是将a数组中的值由高位向低位建Trie后的图,可以看到,两个数之间的异或值就是他们lca节点之后不同的值加上位的权重后的累和,由于只有在lca点才会有两个子节点,进而出现异或出现贡献值的情况,因此,我们dfs到所有的lca(有两个子节点的点),然后贪心向下选取最小不同的值加上位的权重后的累和,最后上代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>

//#define int ll
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 200010, M = 2 * N, B = N, MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int n, m, k;
ll a[N];
int tr[N * 30 + 10][2], cnt;
ll ans = 0;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll mod) {
    ll res = 1;
    while (b) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

inline void init() {}

inline void insert(ll x)
{
    int p = 0;
    rep(i, 30, 0)
    {
        int val = x >> i & 1;
        if (!tr[p][val])
            tr[p][val] = ++cnt;
        p = tr[p][val];
    }
}

ll add(int l, int r, int p)
{
    if (p < 0)return 0;
    ll res1 = -1, res2 = -1;
    if (tr[l][0] && tr[r][0]) res1 = add(tr[l][0], tr[r][0], p - 1);
    if (tr[l][1] && tr[r][1])res2 = add(tr[l][1], tr[r][1], p - 1);
    if (~res1 && ~res2)return min(res1, res2);
    if (~res1)return res1; if (~res2)return res2;

    if (tr[l][0] && tr[r][1])res1 = add(tr[l][0], tr[r][1], p - 1) + (1 << p);
    if (tr[l][1] && tr[r][0])res1 = add(tr[l][1], tr[r][0], p - 1) + (1 << p);
    if (~res1 && ~res2)return min(res1, res2);
    if (~res1)return res1; if (~res2)return res2;
}

void dfs(int u, int p)
{
    if (tr[u][1] && tr[u][0])ans += add(tr[u][0], tr[u][1], p - 1) + (1 << p);
    if (tr[u][0])dfs(tr[u][0], p - 1);
    if (tr[u][1])dfs(tr[u][1], p - 1);
}

void slove()
{
    cin >> n;
    pre(i, 1, n) {
        cin >> a[i];
        insert(a[i]);
    }
    dfs(0, 30);

    cout << ans << endl;
    return;
}

signed main()
{
    int _;
    //si(_);
    _ = 1;
    init();
    while (_--)
    {
        slove();
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值