The third string training

String training three

https://www.luogu.org/problem/P4551

Subject description:

Given an n-point weighted tree index node from a start to N. Looking to find two nodes in the tree, find the longest path or exclusive.

Path is the XOR means exclusive rights to all sides on the unique path between two nodes or

analysis:

Ah? This is not a topic map it? What shit string?

First see different or that 01trie tree on the essential

First, a secondary side or exclusive, no equivalent to the exclusive OR.

In this case i -> j and an exclusive-OR, is i -> 1 and an exclusive-OR, XOR and then the 1 -> j and exclusive OR.

1 each point to the processing path and XOR, and then find the two, so that they together XOR maximum.

Etc. This board is not the question right?

Find two exclusive or maximum (the two exercises have said before me)

So you can directly set the board

code by wzxbeliever

#include<bits/stdc++.h>
#define ll long long
#define ri register int
#define il inline
#define lowbit(x) x&(-x)
using namespace std;
const int maxn=100005;
int head[maxn],tr[maxn*31][2],w[maxn]; 
int n,ans,rt,cnt,tot;
struct node{int to,next,w;}edg[maxn<<1];
il void add(int u,int v,int w){++cnt;edg[cnt].next=head[u];edg[cnt].w=w;edg[cnt].to=v;head[u]=cnt;}
il void bulid(int x,int rt){
     for(ri i=1<<30;i;i>>=1){bool c=x&i;
     if(!tr[rt][c])tr[rt][c]=++tot;
     rt=tr[rt][c];
     }
}
il int query(int x,int rt){
     int ans=0; 
     for(ri i=1<<30;i;i>>=1){bool c=x&i;
     if(tr[rt][c^1])ans+=i,rt=tr[rt][c^1];
     else rt=tr[rt][c];
     }return ans;
}
il void dfs(int u,int fa){
    for(ri i=head[u];i;i=edg[i].next){int to=edg[i].to;
    if(to==fa)continue;
    w[to]=w[u]^edg[i].w;dfs(to,u);
    }
}
int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    scanf("%d",&n);
    for(ri i=1,u,v,w;i<n;i++)scanf("%d%d%d",&u,&v,&w),add(u,v,w),add(v,u,w);
    dfs(1,0);
    for(ri i=1;i<=n;i++)bulid(w[i],rt);
    for(ri i=1;i<=n;i++)ans=max(ans,query(w[i],rt));
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/wzxbeliever/p/11621318.html
Recommended