算法
线性基
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<cstring>

using namespace std;

typedef long long LL;

const int N = 50010, M = 70;

LL h[4 * N], ne[4 * N], e[4 * N], w[4 * N], idx;

LL p[M];

LL dis[N];
bool st[N];
int n, m;

void add(LL a, LL b, LL c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++;
}

// 注意最多是从63位开始,从64位开始就会发生溢出。
void insert(LL x) {
for(int i = 63; ~i; i --) {
if(x >> i & 1) { //这位是1才进行操作
if(p[i])
x ^= p[i];
else {
p[i] = x;
break;
}
}
}

}

void dfs(LL x, LL res) {
dis[x] = res;
st[x] = true;
for(int i = h[x]; i != -1; i = ne[i]) {
int j = e[i];
if(!st[j]) {
dfs(j, res ^ w[i]);
}
else
insert(res ^ w[i] ^ dis[j]);
}
}

int main() {
cin >> n >> m;

memset(h, -1, sizeof(h));

while(m --) {
LL a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
add(a, b, c);
add(b, a, c);

}

dfs(1, 0);

LL res = dis[n];

for(int i = 64; ~i; i --)
if((res ^ p[i]) > res)
res ^= p[i];

cout << res << endl;

return 0;
}