zoukankan      html  css  js  c++  java
  • poj 3764 The xor-longest Path (01 Trie)

    链接:http://poj.org/problem?id=3764

    题面:

    The xor-longest Path
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 11802   Accepted: 2321

    Description

    In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p:

    _{xor}length(p)=oplus_{e in p}w(e)

    ⊕ is the xor operator.

    We say a path the xor-longest path if it has the largest xor-length. Given an edge-weighted tree with n nodes, can you find the xor-longest path?  

    Input

    The input contains several test cases. The first line of each test case contains an integer n(1<=n<=100000), The following n-1 lines each contains three integers u(0 <= u < n),v(0 <= v < n),w(0 <= w < 2^31), which means there is an edge between node u and v of length w.

    Output

    For each test case output the xor-length of the xor-longest path.

    Sample Input

    4
    0 1 3
    1 2 4
    1 3 6
    

    Sample Output

    7

    Hint

    The xor-longest path is 0->1->2, which has length 7 (=3 ⊕ 4)

    思路;

    树上dfs一遍跟区间差不多的写法

    实现代码;

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    #define ll long long
    const int M = 4e5+10;
    int tot;
    int ch[32*M][2],cnt,head[M];
    int val[32*M],a[M],pre[M],nex[M],dp[M],ans;
    
    struct node{
        int to,next;
        ll w;
    }e[M*2];
    
    void add(int u,int v,int w){
        e[++cnt].to = v;e[cnt].w = w;e[cnt].next = head[u];head[u] = cnt;
    }
    
    void init(){
        tot = 1; ans = 0; cnt = 0;
        ch[0][0] = ch[0][1] = 0;
        memset(head,0,sizeof(head));
    }
    
    void ins(ll x){
        int u = 0;
        for(int i = 31;i >= 0;i --){
            int v = (x>>i)&1;
            if(!ch[u][v]){
                ch[tot][0] = ch[tot][1] = 0;
                val[tot] = 0;
                ch[u][v] = tot++;
            }
            u = ch[u][v];
        }
        val[u] = x;
    }
    
    
    int query(int x){
        int u = 0;
        for(int i = 31;i >= 0;i --){
            int v = (x>>i)&1;
            if(ch[u][v^1]) u = ch[u][v^1];
            else u = ch[u][v];
        }
        return x^val[u];
    }
    
    void dfs(int u,int fa,int val){
        ins(val);
        for(int i = head[u];i;i = e[i].next){
            int v = e[i].to;
            if(v == fa) continue;
            ans = max(ans,query(val^e[i].w));
            dfs(v,u,val^e[i].w);
        }
    }
    
    int main()
    {
        int n,u,v,w;
        while(scanf("%d",&n)!=EOF){
        init();
        for(int i = 1;i < n;i++){
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w); add(v,u,w);
        }
        dfs(0,-1,0);
        printf("%d
    ",ans);
        }
    }
  • 相关阅读:
    ps-- 制作磨砂背景
    ps -- 证件照
    抠图--薄,透
    60后发送短信的方法
    小项目 -- 验证码.js
    小项目
    小项目 -- phone.js
    基于GDAL提取地物,并生成png,最后加载到网页上(二)
    根据范围获取影像瓦片,并生成GeoTIFF 文件《一》
    Ubuntu 10.4 +NVIDIA GTX 1070 显卡驱动更新
  • 原文地址:https://www.cnblogs.com/kls123/p/10724745.html
Copyright © 2011-2022 走看看