题解见: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. 在先电IAAS平台中,搭建先电bigdata平台

    以两台节点为例来组件Hadoop分布式集群,这里采用的系统版本为Centos7,如下表所示: 主机名 内存 硬盘 IP地址 角色 master 8192MB 100G 192.168.200.131 ...

  2. jqGrid清空表格

    $("#jqGrid").jqGrid("setGridParam",{ datatype:'local', data : [], page:1 }).trig ...

  3. 记录下为了玩 docker 安装 CentOS 7 最简化版后遇到的一些问题

    今天我的腾讯云服务器在使用docker安装ElasticSearch和Kibana的时候内存不够,安装完直接卡死.所以无奈只能在本地上跑VMWare安装CentOS7来运行测试. 从阿里云镜像地址:h ...

  4. poj1915(双向bfs)

    题目链接:https://vjudge.net/problem/POJ-1915 题意:求棋盘上起点到终点最少的步数. 思路:双向广搜模板题,但玄学的是我的代码G++会wa,C++过了,没找到原因QA ...

  5. #【Python】【基础知识】【内置对象常用方法】

    数字 数字的常用方法: >>> dir(int) ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class ...

  6. 从 .NET 到 JavaScript —— 纯前端报表控件 ActiveReportsJS 焕新登场

    报表工具的发展史,最早可以追溯到微软报表SSRS(SQL Server Reporting Services)时期.最初,报表工具主要应用于报表的定制.呈现和输出.经过几十年的发展,随着各种业务系统功 ...

  7. 小菜鸟之oracle触发器

    1.触发器说明 触发器是一种在事件发生时隐式地自动执行的PL/SQL块,不能接受参数,不能被显式调用 2.触发器类型 根据触发器所创建的语句及所影响的对象的不同,将触发器分为以下3类 (1)DML触发 ...

  8. ES使用小结之索引Rollover

    Elasticsearch 使用小结之索引Rollover 索引名 一般而言,客户端将数据每天写入一个索引,比如直接写入YYYY-MM-HH格式的索引,那么我们只需要在写入的客户端里面获取时间,然后得 ...

  9. 南昌网络赛J. Distance on the tree 树链剖分

    Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...

  10. css 颜色自动变化 炫彩

    .layui-icon-login-qq:hover{ color:rgb(0, 156, 255); transition: 0.5s; animation:change 10s linear 0s ...