• POJ 3764 The XOR Longest Path


    Trie求最大XOR

    设w[x]表示从跟节点到x的路径上所有权值的xor,显然有:

    w[x] = w[fa] xor weight(x, fa)

    所以我们可以先dfs一次树,把所有点的w都预处理出来。。
    根据xor的性质可知,x xor x = 0,所以问题就变成了求w[x] xor w[y]的最大值(因为相同路径都抵消了)
    所以我们把w值用trie维护即可

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    // 为啥不能用万能头呢poj。。。
    #define INF 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C yql){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % yql)if(p & 1)ans = 1LL * x * ans % yql;
        return ans;
    }
    const int N = 100005;
    int trie[6000000][2], tot, cnt, head[N], w[N];
    struct Edge{
        int v, next, w;
    }edge[N<<2];
    
    void addEdge(int a, int b, int w){
        edge[cnt].v = b;
        edge[cnt].w = w;
        edge[cnt].next = head[a];
        head[a] = cnt ++;
    }
    
    void dfs(int s, int fa){
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(u != fa){
                w[u] = w[s]^edge[i].w;
                dfs(u, s);
            }
        }
    }
    
    void insert(int num){
        int cur = 1;
        for(int i = 31; i >= 0; i --){
            int p = (num >> i) & 1;
            if(trie[cur][p] == 0) trie[cur][p] = ++tot;
            cur = trie[cur][p];
        }
    }
    
    int search(int num){
        int cur = 1, ret = 0;
        for(int i = 31; i >= 0; i --){
            int p = (num >> i) & 1;
            if(trie[cur][p^1] == 0) cur = trie[cur][p];
            else ret += (1 << i), cur = trie[cur][p^1];
        }
        return ret;
    }
    
    int main(){
    
        int n;
        while(scanf("%d", &n) != EOF){
            cnt = 0;
            memset(head, -1, sizeof head);
            memset(trie, 0, sizeof trie);
            memset(w, 0, sizeof w);
            for(int i = 0; i < n - 1; i++){
                int a = read(), b = read(), c = read();
                addEdge(a, b, c), addEdge(b, a, c);
            }
            dfs(0, 0);
            int ans = 0;
            tot = 1;
            for(int i = 0; i < n; i++){
                insert(w[i]);
                ans = max(ans, search(w[i]));
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    
    
  • 相关阅读:
    mysql相关
    java注解@Valid@Validated表单验证
    驼峰参数、下划线["_"]参数,属性互传
    redis,windows设置记录
    Java入门第一季学习总结
    计算属性calc()的运算规则
    swiper实现翻页,页面高度超出可滚动
    git常用命令
    Linux下svn常用命令
    图片高度不够一页,如何覆盖全屏
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10512966.html
Copyright © 2020-2023  润新知