线性基

首先我们发现,对于一条路径走过去再走回来是没有意义的,

所以我们可以没有任何其他影响的取得一个环的异或和

所以我们预处理出来所有环的异或和,求出他们的线性基,然后任找一条 \(1 \sim n\) 的路径,找出异或和的最大值

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define ll long long
#define MB 62
using namespace std;
const int MAXN = 400005;
struct edge{
int to, nxt;
ll dis;
}e[MAXN<<1];
int head[MAXN], nume, n, m, tot;
ll a[MAXN], lb[MAXN], d[MAXN];
bool f[MAXN];
void adde(int from, int to, ll dis) {
e[++nume].to = to;
e[nume].dis = dis;
e[nume].nxt = head[from];
head[from] = nume;
}
ll init() {
ll rv = 0, fh = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') fh = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
rv = (rv<<1) + (rv<<3) + c - '0';
c = getchar();
}
return fh * rv;
}
void dfs(int u, int fa) {
f[u] = 1;
for(int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if(v != fa &&!f[v]) {
d[v] = d[u] ^ e[i].dis;
dfs(v, u);
}else if(v != fa) {
a[++tot] = d[u] ^ d[v] ^ e[i].dis;
}
}
}
void prepare(){
for(int i = 1; i <= tot; i++) {
for(int j = MB; j >= 0; j--) {
if(a[i] & (1ll << j)) {
if(!lb[j]){
lb[j] = a[i];
for(int k = j - 1; k >= 0; k--) if(lb[k] && (lb[j] & (1ll << k))) lb[j] ^= lb[k];
for(int k = j + 1; k <= MB; k++) if(lb[k] & (1ll << j)) lb[k] ^= lb[j];
break;
}else a[i] ^= lb[j];
}
}
}
}
int main() {
n = init(); m = init();
for(int i = 1; i <= m; i++) {
int u = init(), v = init();
ll dis = init();
adde(u, v, dis); adde(v, u, dis);
}
dfs(1, 0);
prepare();
ll ans = d[n];
for(int i = MB; i >= 0; i--) {
if((ans ^ lb[i]) > ans) ans ^= lb[i];
}
cout<<ans<<endl;
return 0;
}
05-24 17:27