题解见: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. sed工具的基本用法

    sed文本处理工具的用法: 用法1:前置命令 | sed [选项] '条件指令' 用法2:sed [选项] '条件指令' 文件.. .. 认识sed工具的基本选项 sed命令的常用选项如下: -n(屏 ...

  2. 如何运行spring boot 工程

    1.右键工程,Run As, Maven install, 2.右键工程,Run As,Spring Boot App 3.在地址栏输入127.0.0.1:8080 动图示例

  3. elasticsearch备份脚本

    1.主要文件 [root@k8s elasticsearch]# tree . ├── backup_es.sh ├── indices_file.txt ├── recover_es.sh └── ...

  4. 汉诺塔递推HDU2064

    题意: 移动木头盘不能a到c,必须a到b到c. 问你移动次数. 假设将n层塔从A经B挪到C需要f[n]步.那么具体的移动过程可以这样看:将上面n-1层从A经B挪到C需要f[n-1]步,再将第n层从A挪 ...

  5. python+selenium+webdriver+BeautifulSoup实现自动登录

    from selenium import webdriverimport timefrom bs4 import BeautifulSoupfrom urllib import requestimpo ...

  6. python基础(十)--函数进阶

    嵌套函数 >>> graphic = '三角形' >>> def chang(): graphic = '正方形' def chang1(): #内部嵌套的函数命名 ...

  7. 以前面试 经常写这种 问掉的 copy 还是 =

    get的时候,生成的  那个对象赋值给aa 生成的对象在这条语句完  就析构了: https://blog.csdn.net/qq_31759205/article/details/80544468h ...

  8. 用Lua的协程实现类似Unity协程的语句块

    local co_time_tbl = {} setmetatable(co_time_tbl, { __len = function(o) for k, v in pairs(o) do count ...

  9. CSS 定位 四种定位

    absolute  生成绝对定位的元素,相对于static定位以外的第一个父元素进行定位.元素的位置通过“left”,“top”,“right”以及“bottom”属性进行定位. fixed 生成固定 ...

  10. WinPE基础知识之代码解析

    void CMyPE::OnClickedButton1() { // TODO: 在此添加控件通知处理程序代码 // 打开一个文件夹选择对话框 CFileDialog dlg(TRUE); dlg. ...