题解见: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[];
bool insert(LL x) {
for (int i = ; i >= ; i--)
if ((x >> i) & ) {
if (!num[i]) {
num[i] = x;
return true;
}
x ^= num[i];
}
return false;
}
LL query(LL x) {
LL res = x;
for (int i = ; i >= ; i--)
if ((res ^ num[i]) > res)
res ^= num[i];
return res;
}
struct edge {
int to, next;
LL w;
} e[];
int head[], 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[];
LL del[];
void dfs(int u, LL res) {
del[u] = res, vis[u] = ;
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 = ; i <= m; i++)
scanf("%d%d%lld", &a, &b, &c), adde(a, b, c);
dfs(, );
printf("%lld\n", query(del[n]));
}

P4151 最大XOR和路径 线性基的更多相关文章

  1. 洛谷P4151 [WC2011] 最大XOR和路径 [线性基,DFS]

    题目传送门 最大XOR和路径 格式难调,题面就不放了. 分析: 一道需要深刻理解线性基的题目. 好久没打过线性基的题了,一开始看到这题还是有点蒙逼的,想了几种方法全被否定了.还是看了大佬的题解才会做的 ...

  2. [WC2011]最大XOR和路径 线性基

    [WC2011]最大XOR和路径 LG传送门 需要充分发掘经过路径的性质:首先注意不一定是简单路径,但由于统计的是异或值,重复走是不会被统计到的,考虑对于任意一条从\(1\)到\(n\)的路径的有效部 ...

  3. [luogu4151 WC2011] 最大XOR和路径 (线性基)

    传送门 输入输出样例 输入样例#1: 5 7 1 2 2 1 3 2 2 4 1 2 5 1 4 5 3 5 3 4 4 3 2 输出样例#1: 6 说明 [样例说明] 根据异或的性质,将一个数异或两 ...

  4. P4151 [WC2011]最大XOR和路径 线性基

    题目传送门 题意:给出一幅无向图,求1到n的所有路径中最大异或和,一条边可以被重复经过. 思路: 参考了大佬的博客 #pragma GCC optimize (2) #pragma G++ optim ...

  5. 洛谷P4151 最大XOR和路径 [WC2011] 线性基+图论

    正解:线性基+图论 解题报告: 传送门 首先可以思考一下有意义的路径会是什么样子,,,那就一定是一条链+一些环 挺显然的因为一条路径原路返回有没有意义辣?所以一定是走一条链+一些环(当然也可以麻油环, ...

  6. CF1101G (Zero XOR Subset)-less 线性基

    传送门 既然每一次选择出来的都是一个子段,不难想到前缀和计算(然而我没有想到--) 设异或前缀和为\(x_i\),假设我们选出来的子段为\([1,i_1],(i_1,i_2],...,(i_{k-1} ...

  7. CodeForces - 1101G :(Zero XOR Subset)-less(线性基)

    You are given an array a1,a2,…,an of integer numbers. Your task is to divide the array into the maxi ...

  8. 牛客练习赛26 D xor序列 (线性基)

    链接:https://ac.nowcoder.com/acm/contest/180/D 来源:牛客网 xor序列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他 ...

  9. 2019年牛客多校第四场 B题xor(线段树+线性基交)

    题目链接 传送门 题意 给你\(n\)个基底,求\([l,r]\)内的每个基底是否都能异或出\(x\). 思路 线性基交板子题,但是一直没看懂咋求,先偷一份咖啡鸡板子写篇博客吧~ 线性基交学习博客:传 ...

随机推荐

  1. 修改阿里源为Ubuntu 18.04默认的源

    步骤如下: Step1:备份/etc/apt/sources.list sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak Step2:在/ ...

  2. 【ARM-Linux开发】Linux内存管理:ARM Memory Layout以及mmu配置

    原文:Linux内存管理:ARM Memory Layout以及mmu配置 在内核进行page初始化以及mmu配置之前,首先需要知道整个memory map. 1. ARM Memory Layout ...

  3. lua table 的操作(四)

    table在前面作过介绍,它是一种关联数组,这种关联指的是可以设置各类类型的key来存储值. 1.table 间的数据传递 -- 为 table a 并设置元素,然后将 a 赋值给 b,则 a 与 b ...

  4. [转帖]linux各种IPC机制

    linux各种IPC机制 docker中的资源隔离,一种就是IPC的隔离.IPC是进程间通信. 下面的文章转载自https://blog.csdn.net/yyq_9623/article/detai ...

  5. 题目15 链表中倒数第K个节点

    ///////////////////////////////////////////////////////////////////////////////////// // 5. 题目15 链表中 ...

  6. Caesar's Legions(CodeForces-118D) 【DP】

    题目链接:https://vjudge.net/problem/CodeForces-118D 题意:有n1名步兵和n2名骑兵,现在要将他们排成一列,并且最多连续k1名步兵站在一起,最多连续k2名骑兵 ...

  7. thinkphp5.1中使用Bootstrap4分页样式修改

    1.找到thinkphp下的Boorstrap的源码 \thinkphp\library\think\paginator\driver\Bootstrap.php 2丶直接修改源码 <?php ...

  8. Python 运算符与数据类型

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...

  9. c# 粘贴复制

    复制 1. 复制 Clipboard.SetText("123456"); Clipboard.SetImage(Image img); Clipboard.SetAudio(Sy ...

  10. element随笔

    时间选择框el-date-picker和select框数据选不上: [解决]用v-model="searchData.searchDate",不能用:model="sea ...