dfs出任意一条到n的异或和,以及所有环的异或和,线性基求最大值。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, tl, hed[200005], tot;
ll huan[200005], d[200005], p[100];
bool vis[200005];
struct node {
int v, nxt;
ll w;
}e[200005];
void add(int u, int v, ll w) {
e[++tl].v = v;
e[tl].nxt = hed[u];
e[tl].w = w;
hed[u] = tl;
}
void dfs(int u) {
int i, v;
vis[u] = 1;
for(i = hed[u]; ~i; i = e[i].nxt) {
v = e[i].v;
if(vis[v]) huan[++tot] = d[v] ^ e[i].w ^ d[u];
else d[v] = d[u] ^ e[i].w, dfs(v);
}
}
void xxj() {
ll i, j;
for(i = 1; i <= tot; ++i)
for(j = 62; j >= 0; --j)
if((huan[i] >> j) & 1) {
if(!p[j]) {
p[j] = huan[i];
break;
}
else huan[i] ^= p[j];
}
for(i = 62; i >= 0; --i) if((d[n] ^ p[i]) > d[n]) d[n] ^= p[i];
cout << d[n] << endl;
}
int main() {
int i, u, v;
ll w;
cin >> n >> m;
memset(hed, -1,sizeof(hed));
for(i = 1; i <= m; ++i) {
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
dfs(1);
xxj();
return 0;
}