The XOR Largest Path(trie、poj3764)

The XOR Largest Path

The XOR Largest Path
为什么牛客过了poj没过我好菜啊

int n;
int trie[maxn*32+5][2],tot=1,D[maxn];
vector<int> G[maxn],W[maxn];
void inser(int a){
    
    //trie插入
    int p=1;
    int ch=1;
    for(int i=31;i>=0;i--){
    
    
        ch=a>>i&1;
        if(trie[p][ch]==0)trie[p][ch]=++tot;
        p=trie[p][ch];
    }
}
int sear(int a){
    
    
    int p=1;
    int ch=1,ans=0;
    for(int i=31;i>=0;i--){
    
    
        ch=a>>i&1;
        if(trie[p][ch^1]){
    
    
            p=trie[p][ch^1];
            ans|=1<<i;
        }else{
    
    
            p=trie[p][ch];
        }
    }
    return ans;
}
void in(int n){
    
    
    int u,v;
    int w;
    for(int i=1;i<n;i++){
    
    
        scanf("%d%d%d",&u,&v,&w);
        G[u].push_back(v);
        G[v].push_back(u);
        W[u].push_back(w);
        W[v].push_back(w);
    }
}
void dfs(int nw,int fa,int we){
    
    
    int tt;
    D[nw]=D[fa]^we;
    for(int i=0;i<G[nw].size();i++){
    
    
        tt=G[nw][i];
        if(tt==fa)continue;
        dfs(tt,nw,W[nw][i]);
    }
}
int main(){
    
    
    int n;
    sci(n);
    in(n);
    int res=0;
    mem(trie,0);
    mem(D,0);
    dfs(0,0,0);
    for(int i=0;i<n;i++){
    
    
        inser(D[i]);
        res=max(res,sear(D[i]));
    }
    cout<<res<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44986601/article/details/105629333