[WC2011]最大XOR和路径

LG传送门

需要充分发掘经过路径的性质:首先注意不一定是简单路径,但由于统计的是异或值,重复走是不会被统计到的,考虑对于任意一条从\(1\)到\(n\)的路径的有效部分是什么。最简单的情况就是走一条链,有时候我们会从这条链走出去,走一段路径之后走一个环,再沿这条路径回到原来的链上,这样一来答案就变成了原来的链异或找到的环。我们发现任意的环都可以用来更新答案,那么我们找到原图中所有的环丢进线性基里,再把所有一条\(1\)到\(n\)的链在线性基里查询最大异或和就行了。但事实上最后一步我们只要用任意一条\(1\)到\(n\)的链来查询。

你可能会对于我们任意找的一条链的最优性有疑问,事实上原图上\(1\)到\(n\)之间的所有链都可以通过其中的任意一条异或某一个环得到,所以事实上我们还是找出了所有的链。

//written by newbiechd
#include <cstdio>
#include <cctype>
#define R register
#define I inline
#define B 1000000
#define L long long
using namespace std;
const int N = 50003, M = 200003, S = 64;
char buf[B], *p1, *p2;
I char gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, B, stdin), p1 == p2) ? EOF : *p1++; }
I L rd() {
R L f = 0;
R char c = gc();
while (c < 48 || c > 57)
c = gc();
while (c > 47 && c < 58)
f = f * 10 + (c ^ 48), c = gc();
return f;
}
int vis[N], h[N], E;
L val[N], base[S];
struct edge {
int g, s;
L w;
}e[M];
I L max(L x, L y) { return x > y ? x : y; }
I void add(int x, int y, L z) { e[++E] = (edge){y, h[x], z}, h[x] = E; }
I void insert(L x) {
for (R int i = S - 1; ~i; --i)
if ((x >> i) & 1) {
if (!base[i])
base[i] = x;
x ^= base[i];
}
}
I L query(L x) {
for (R int i = S - 1; ~i; --i)
x = max(x, x ^ base[i]);
return x;
}
void dfs(int x) {
for (R int i = h[x], y; i; i = e[i].s)
if (!vis[y = e[i].g])
vis[y] = 1, val[y] = val[x] ^ e[i].w, dfs(y);
else
insert(val[y] ^ val[x] ^ e[i].w);
}
int main() {
R int n = rd(), m = rd(), i, x, y;
L z;
for (i = 1; i <= m; ++i)
x = rd(), y = rd(), z = rd(), add(x, y, z), add(y, x, z);
vis[1] = 1, dfs(1), printf("%lld", query(val[n]));
return 0;
}

最新文章

  1. 安装MariaDB和简单配置
  2. 【BZOJ】3835: [Poi2014]Supercomputer
  3. table 固定表头
  4. node.js问题二
  5. scp命令[转]
  6. 通信原理实践(二)&mdash;&mdash;幅度调制
  7. java.net.SocketException: No buffer space available
  8. git 命令的学习
  9. 用命令实现Win7远程桌面关机和重启
  10. android 使用静态变量传递数据
  11. SQL Cast()函数
  12. java读写
  13. Head First - 01.策略模式(Strategy Pattern)
  14. 4、手把手教你Extjs5(四)主界面上加入顶部和底部区域
  15. jQuery选择器 :eq() 不能识别变量参数的问题解决方案
  16. Educational Codeforces Round 61 Editorial--C. Painting the Fence
  17. 4-23 模块 hashlib ,configparser,loging,collections
  18. 关于$\mathcal{D}(0,1)$上的一个有趣结论
  19. 2-3 用组件改写Todolist案例
  20. Android/Java 中的 String, StringBuffer, StringBuilder的区别和使用

热门文章

  1. iOS设计模式 - 中介者
  2. Linux 系统磁盘挂载信息文件
  3. Linux setfacl/getfacl命令详解
  4. zabbix日常监控项java(四)
  5. MapReduce实例&amp;YARN框架
  6. codeforces 17D Notepad
  7. QT里使用Gsoap调用WebService
  8. PHP Redis 全部操作方法 转载
  9. 纯css3云彩动画效果
  10. Java8系列之重新认识HashMap(转)