forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3764 - The xor-longest Path.cpp
81 lines (58 loc) · 1.53 KB
/
3764 - The xor-longest Path.cpp
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
78
79
80
81
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXV 100000
#define MAXE 2 * MAXV
int to[MAXE],W[MAXE],nxt[MAXE],last[MAXV],E;
int sum[MAXV],trie[MAXV * 31][2],cont;
void add_edge(int u, int v, int w){
to[E] = v; W[E] = w; nxt[E] = last[u]; last[u] = E++;
to[E] = u; W[E] = w; nxt[E] = last[v]; last[v] = E++;
}
void insert(int s){
int pos = 0;
for(int i = 30,k;i >= 0;--i){
if(s & (1 << i)) k = 1;
else k = 0;
if(trie[pos][k] == -1) trie[pos][k] = cont++;
pos = trie[pos][k];
}
}
void dfs(int cur, int par, int s){
sum[cur] = s;
insert(s);
for(int e = last[cur];e != -1;e = nxt[e])
if(to[e] != par)
dfs(to[e],cur,s ^ W[e]);
}
int solve(int s){
int ret = 0,pos = 0;
for(int i = 30,k;i >= 0;--i){
if(s & (1 << i)) k = 0;
else k = 1;
if(trie[pos][k] == -1) k ^= 1;
else ret ^= (1 << i);
pos = trie[pos][k];
}
return ret;
}
int main(){
int N;
while(scanf("%d",&N) == 1){
memset(last,-1,sizeof last);
E = 0;
for(int i = 1,u,v,w;i < N;++i){
scanf("%d %d %d",&u,&v,&w);
add_edge(u,v,w);
}
memset(trie,-1,sizeof trie);
cont = 1;
dfs(0,-1,0);
int ans = 0;
for(int i = 0;i < N;++i)
ans = max(ans,solve(sum[i]));
printf("%d\n",ans);
}
return 0;
}