zoukankan      html  css  js  c++  java
  • P4151 最大XOR和路径 线性基

    题解见:https://www.luogu.org/problemnew/solution/P4151

    其实就是找出所有环 把环上所有边异或起来得到的值扔到线性基里面

    然后随便走一条从1~n的链 最后求最大异或和即可

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define LL long long
    LL num[70];
    bool insert(LL x) {
            for (int i = 63; i >= 0; i--)
                    if ((x >> i) & 1) {
                            if (!num[i]) {
                                    num[i] = x;
                                    return true;
                            }
                            x ^= num[i];
                    }
            return false;
    }
    LL query(LL x) {
            LL res = x;
            for (int i = 63; i >= 0; i--)
                    if ((res ^ num[i]) > res)
                            res ^= num[i];
            return res;
    }
    struct edge {
            int to, next;
            LL w;
    } e[200010];
    int head[50010], ecnt;
    inline void adde(int from, int to, LL w) {
            e[++ecnt] = (edge) {to, head[from], w}, head[from] = ecnt;
            e[++ecnt] = (edge) {from, head[to], w}, head[to] = ecnt;
    }
    int vis[50010];
    LL del[50010];
    void dfs(int u, LL res) {
            del[u] = res, vis[u] = 1;
            for (int i = head[u]; i; i = e[i].next)
                    if (!vis[e[i].to])
                            dfs(e[i].to, res ^ e[i].w);
                    else
                            insert(res ^ e[i].w ^ del[e[i].to]);
    }
    int main() {
            int n, m, a, b;
            LL c;
            scanf("%d%d", &n, &m);
            for (int i = 1; i <= m; i++)
                    scanf("%d%d%lld", &a, &b, &c), adde(a, b, c);
            dfs(1, 0);
            printf("%lld
    ", query(del[n]));
    }
  • 相关阅读:
    20190905-3 命令行和控制台编程
    作业要求 20181009-9 每周例行报告
    每周例行报告
    单元测试,结对
    四则运算试题生成
    代码规范,结对要求
    规格说明书-吉林市2日游
    功能测试
    每周例行报告2
    get与post请求的区别
  • 原文地址:https://www.cnblogs.com/Aragaki/p/11229205.html
Copyright © 2011-2022 走看看