发现 1 -- n 的最小异或和路径可以用某一条 1 -- n 的路径 异或 随便一些环的最小值表示
因为如果走到那些环更优,我们可以先走到那个环,然后走一圈,再走回来
过去回来的贡献会被抵消掉,于是我们可以处理出所有环的异或值插入线性基,然后查询与 n 异或最小的即可
#include<bits/stdc++.h>
#define N 200050
using namespace std;
int first[N], nxt[N], to[N], w[N], tot;
void add(int x, int y, int z){
nxt[++tot] = first[x], first[x] = tot;
to[tot] = y, w[tot] = z;
}
int n, m;
bool vis[N]; int dis[N], A[50];
void Insert(int x){
for(int i = 30; i >= 0; i--){
if((1<<i) & x){
if(A[i]) x ^= A[i];
else { A[i] = x; break;}
}
}
}
void dfs(int u){
vis[u] = true;
for(int i=first[u];i;i=nxt[i]){
int t = to[i];
if(vis[t]) Insert(dis[t] ^ dis[u] ^ w[i]);
else dis[t] = dis[u] ^ w[i], dfs(t);
}
}
int main(){
scanf("%d%d", &n, &m);
for(int i=1; i<=m; i++){
int x, y, z; scanf("%d%d%d", &x, &y, &z);
add(x, y, z); add(y, x, z);
} dfs(1);
for(int i=30; i>=0; i--) if((dis[n] ^ A[i]) < dis[n]) dis[n] ^= A[i];
printf("%d", dis[n]); return 0;
}