#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e5 + 5;
struct Trie {
int ch[N << 4][2], tot, id[N << 4];
void init () {
for (int i = 1; i <= tot; i ++) id[i] = ch[i][0] = ch[i][1] = 0;
tot = 1;
}
void insert (int x, int ID) {
int p = 1;
for (int i = 32; i >= 0; i --) {
int j = (x >> i) & 1;
if (ch[p][j] == 0) ch[p][j] = ++ tot;
p = ch[p][j];
}
id[p] = ID;
}
int ask_val (int x) {
int p = 1, ans = 0;
for (int i = 32; i >= 0; i --) {
int j = ((x >> i) & 1) ^ 1;
if (ch[p][j] == 0) j ^= 1;
if (ch[p][j] == 0) return 0;
ans |= (j << i); p = ch[p][j];
}
return ans;
}
int ask_ID (int x) {
int p = 1;
for (int i = 32; i >= 0; i --) {
int j = ((x >> i) & 1) ^ 1;
if (ch[p][j] == 0) j ^= 1;
p = ch[p][j];
}
return id[p];
}
} A, B;
int Xor[N], head[N], number, f[N], dfn[N], V[N], siz[N], gg, L[N], R[N], out[N], in[N];
bool vis[N];
struct node {
int w, next, to;
} g[N];
void add (int x, int y, int z) { g[++ number] = {z, head[x], y}; head[x] = number; }
void dfs (int x, int fa) {
f[x] = fa; siz[x] = 1; dfn[x] = ++ gg; V[gg] = x;
for (int i = head[x]; i; i = g[i].next) {
int to = g[i].to, w = g[i].w;
if (to == fa) continue;
Xor[to] = Xor[x] ^ w;
dfs (to, x); siz[x] += siz[to];
}
L[x] = dfn[x], R[x] = dfn[x] + siz[x] - 1;
}
stack <int> s;
int nxt[N];
void Get_Mark (int x) {
B.init (); int ans = 0;
while (x != 0) {
B.insert (Xor[x], x); ans = max (ans, Xor[x] ^ B.ask_val (Xor[x]));
for (int i = head[x]; i; i = g[i].next) {
int to = g[i].to;
if (to == f[x] or to == nxt[x]) continue;
for (int j = L[to]; j <= R[to]; j ++) {
int id = V[j];
B.insert (Xor[id], id), ans = max (ans, Xor[id] ^ B.ask_val (Xor[id]));
}
}
in[x] = ans;
vis[x] = true;
s.emplace (x);
nxt[f[x]] = x;
x = f[x];
}
ans = 0; B.init ();
while (!s.empty ()) {
int a = s.top (); s.pop ();
out[a] = ans;
B.insert (Xor[a], a); ans = max (ans, Xor[a] ^ B.ask_val (Xor[a]));
for (int i = head[a]; i; i = g[i].next) {
int to = g[i].to;
if (to == f[a] or to == nxt[a]) continue;
for (int j = L[to]; j <= R[to]; j ++) {
B.insert (Xor[V[j]], V[j]);
ans = max (ans, Xor[V[j]] ^ B.ask_val (Xor[V[j]]));
}
}
}
}
void doit (int x) {
B.init (); int ans = 0;
for (int i = L[x]; i <= R[x]; i ++) {
int j = V[i];
B.insert (Xor[j], j); ans = max (ans, Xor[j] ^ B.ask_val (Xor[j]));
}
in[x] = ans;
}
void dfs2 (int x) {
for (int i = head[x]; i; i = g[i].next) {
int to = g[i].to;
if (to == f[x]) continue;
if (vis[to] == false) doit (to);
else dfs2 (to);
}
}
signed main () {
//freopen ("in.in", "r", stdin);
// freopen ("out.out", "w", stdout);
int n; cin >> n;
for (int i = 1; i < n; i ++) {
int x, y, z; cin >> x >> y >> z;
add (x, y, z); add (y, x, z);
}
dfs (1, 0); A.init ();
for (int i = 1; i <= n; i ++)
A.insert (Xor[i], i);
int X, Y, Mx = -1e9;
for (int i = 1; i <= n; i ++) {
int j = A.ask_ID (Xor[i]);
if (Xor[i] ^ Xor[j] > Mx) X = i, Y = j, Mx = Xor[i] ^ Xor[j];
}
Get_Mark (X), Get_Mark (Y); dfs2 (1);
int ans = 0;
for (int i = 1; i <= n; i ++) {
if (vis[i] == false) ans = max (ans, in[i] + Mx);
else ans = max (ans, in[i] + out[i]);
}
cout << ans << "\n";
}
每个subtask都错了一点