zoukankan      html  css  js  c++  java
  • [WC2011]最大XOR和路径

    Description

    直接看标题就行

    Solution

    Xor这种东西,一般就是Trie或者线性基,这个题是线性基。
    每个环都可以走到,而且来回的路上不会产生额外的代价,所以为每个环的Xor和建立线性基。算答案的时候随便选一条从1到n的路径就行,因为如果有其他路径,这两条路径一定成环,在线性基里求最值的时候就知道走那个更优了。

    Code

    #include <cstdio>
    
    typedef long long LL;
    const int B = 62;
    const int N = 50010;
    const int M = 200010;
    
    int hd[N], nxt[M], to[M], cnt, n, m, vis[N];
    LL w[M], del[N];
    LL Lb[B];
    
    void ins(LL x) {
        // printf("%I64d
    ", x);
        for (int i = 60; i>=0; --i) {
            if (!(x>>i) & 1) continue;
            if (!Lb[i]) {
                Lb[i] = x;
                break;
            } else x ^= Lb[i];
        }
    }
    
    LL query(LL x) {
        for (int i = 60; i >= 0; --i) {
            if (x < (x^Lb[i])) x ^= Lb[i];
        }
        return x;
    }
    
    void dfs(int x, LL res) {
        vis[x] = 1; del[x] = res;
        for (int i = hd[x]; i; i = nxt[i])
            if (vis[to[i]]) ins(res^w[i]^del[to[i]]);
            else dfs(to[i], res^w[i]);	
    }
    
    inline void adde(int x, int y, LL z) {
        to[++cnt] = y;
        nxt[cnt] = hd[x]; w[cnt] = z;
        hd[x] = cnt;
    }
    
    int main() {
        scanf("%d%d", &n, &m);
        int x, y; LL z;
        for (int i = 1; i <= m; ++i) {
            scanf("%d%d%lld", &x, &y, &z);
            adde(x, y, z);
            adde(y, x, z);
        }
        dfs(1, 0);
        printf("%lld
    ", query(del[n]));
        return 0;
    }
    
  • 相关阅读:
    java 包
    数据库查询操作练习
    solr全文检索实现原理
    前端页面设计问题小计
    送给自己的九封信
    bootstrap-table初使用
    bootstrap-treeview初使用
    windows:plsql配置oracle连接
    maven的安装和配置
    cxf+spring+restful简单接口搭建
  • 原文地址:https://www.cnblogs.com/wyxwyx/p/wc2011xor.html
Copyright © 2011-2022 走看看