2020牛客多校第5场B-Graph完全图异或最小生成树

2020牛客多校第5场B-Graph完全图异或最小生成树_第1张图片


因为这里边是可以任意加的但是要保证联通性所有这里可以看成一个完全图,每条边的权值=a[u]^a[v]
1.那么我们可以将每个点的权值sort一遍,将每个a[i],从高位到低位,按照最高位的1出现的顺序进行分成若干个集合,然后再最高位相同的集合里面再分次高位以此类推下去:分治的思想。先将集合内的连边再连集合外面的。
2.要维护异或最小值:字典树


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
// #define mid ((l + r) >> 1) 
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define hash Hash
#define next Next
#define count Count
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 2e5+10, mod = 1e9 + 9;
const long double eps = 1e-5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) 
{
    read(first);
    read(args...);
}

struct node {
    int to, next, w;
}e[N];
int head[N], cnt;
ll ans=0;
int trie[N*30][2],tot,n,a[N];

inline void add(int from, int to, int w)
{
    e[cnt] = {to,head[from],w};
    head[from] = cnt++;
}

inline void Dfs(int u, int fa)
{
    for(int i = head[u]; ~i; i = e[i].next)
    {
        int v = e[i].to;
        if(v == fa) continue;
        int val = e[i].w;
        a[v] = a[u] ^ val; 
        Dfs(v,u);
    }
}


void Insert(int x){
    int rt=0;
    for(int i=29;i>=0;i--){
        int now=(x>>i)&1;
        if(!trie[rt][now]) trie[rt][now]=++tot;
        rt=trie[rt][now];
    }
}
int Search(int x){
    int ans=0,rt=0;
    for(int i=29;i>=0;i--){
        int now=(x>>i)&1;
        if(trie[rt][now]){
            rt=trie[rt][now];
        }
        else{
            rt=trie[rt][now^1];
            ans|=(1<<i);
        }
    }
    return ans;
}
void dfs(int l,int r,int dep){
    if(dep==-1||l>=r) return ;
    int mid=l-1;
    while(mid<r&&((a[mid+1]>>dep)&1)==0) mid++;
 
    dfs(l,mid,dep-1);
    dfs(mid+1,r,dep-1);
    if(mid==l-1||mid==r) return ;
    for(int i=l;i<=mid;i++) {
        Insert(a[i]);
    }
    int tmp=INT_MAX;
    for(int i=mid+1;i<=r;i++) {
        tmp=min(tmp,Search(a[i]));
    }
    ans+=(ll)tmp;
    for(int i=0;i<=tot;i++) {
        trie[i][0]=trie[i][1]=0;
    }
    tot=0;
}

int main()
{
    ms(head,-1);
    read(n);
    for(int i = 0; i < n - 1; ++ i)
    {
        int l, r, w;
        read(l,r,w);
        l ++, r ++;
        add(l,r,w);
        add(r,l,w);
    }
    a[1] = 0;
    Dfs(1,0);
    sort(a+1,a+n+1);
    dfs(1,n,29);
    cout << ans << endl;
    return 0;
}

你可能感兴趣的:(树)