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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值