注意到对于树边 $(x, y)$,$d(y)$ 表示根节点到 $y$ 的距离,那么有
$d(y) = d(x) \oplus e(x, y)$,由于异或操作对于路径重复的部分,值为 $0$
所以 $\oplus (x \to y)$ 实际上就是 $d(x) \oplus d(y)$
只要通过 $\text{dfs}$ 预处理出所有的 $d[x]$,然后转换成最大异或对问题求解即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <bitset>
#include <assert.h>
#include <unordered_map>
using namespace std;
typedef long long ll;
#define Cmp(a, b) memcmp(a, b, sizeof(b))
#define Cpy(a, b) memcpy(a, b, sizeof(b))
#define Set(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define _forS(i, l, r) for(set<int>::iterator i = (l); i != (r); i++)
#define _rep(i, l, r) for(int i = (l); i <= (r); i++)
#define _for(i, l, r) for(int i = (l); i < (r); i++)
#define _forDown(i, l, r) for(int i = (l); i >= r; i--)
#define debug_(ch, i) printf(#ch"[%d]: %d\n", i, ch[i])
#define debug_m(mp, p) printf(#mp"[%d]: %d\n", p->first, p->second)
#define debugS(str) cout << "dbg: " << str << endl;
#define debugArr(arr, x, y) _for(i, 0, x) { _for(j, 0, y) printf("%c", arr[i][j]); printf("\n"); }
#define _forPlus(i, l, d, r) for(int i = (l); i + d < (r); i++)
#define lowbit(i) (i & (-i))
#define MPR(a, b) make_pair(a, b)
pair<int, int> crack(int n) {
int st = sqrt(n);
int fac = n / st;
while (n % st) {
st += 1;
fac = n / st;
}
return make_pair(st, fac);
}
inline ll qpow(ll a, int n) {
ll ans = 1;
for(; n; n >>= 1) {
if(n & 1) ans *= 1ll * a;
a *= a;
}
return ans;
}
template <class T>
inline bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll ksc(ll a, ll b, ll mod) {
ll ans = 0;
for(; b; b >>= 1) {
if (b & 1) ans = (ans + a) % mod;
a = (a * 2) % mod;
}
return ans;
}
ll ksm(ll a, ll b, ll mod) {
ll ans = 1 % mod;
a %= mod;
for(; b; b >>= 1) {
if (b & 1) ans = ksc(ans, a, mod);
a = ksc(a, a, mod);
}
return ans;
}
template <class T>
inline bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
template<class T>
bool lexSmaller(vector<T> a, vector<T> b) {
int n = a.size(), m = b.size();
int i;
for(i = 0; i < n && i < m; i++) {
if (a[i] < b[i]) return true;
else if (b[i] < a[i]) return false;
}
return (i == n && i < m);
}
// ============================================================== //
const int maxn = 3100000 + 5;
class Graph {
public:
int n;
int tot = 1;
vector<int> head, e, ne, ver;
Graph() = default;
Graph(int _n) : n(_n) {
tot = 1;
head.resize(n), e.resize(n<<1), ne.resize(n<<1), ver.resize(n<<1);
}
void add(int a, int b, int c) {
ver[++tot] = b; e[tot] = c; ne[tot] = head[a]; head[a] = tot;
}
};
const int N = 100000 + 5;
int n, d[N];
Graph g(N);
class Trie {
public:
int t[maxn][2];
int tot;
Trie() {
tot = 1;
memset(t, 0, sizeof t);
}
void insert(int x) {
int p = 1;
for (int i = 30; i >= 0; i--) {
int c = x >> i & 1;
if (!t[p][c]) t[p][c] = ++tot;
p = t[p][c];
}
}
int query(int x) {
int res = 0, p = 1;
for (int i = 30; i >= 0; i--) {
int c = x >> i & 1;
if (t[p][!c]) p = t[p][!c], res += (1<<i);
else p = t[p][c];
}
return res;
}
} trie;
void dfs(int x, int fa) {
for (int i = g.head[x]; i; i = g.ne[i]) {
int y = g.ver[i];
if (y == fa) continue;
d[y] = d[x] ^ g.e[i];
dfs(y, x);
}
}
int main() {
//freopen("input.txt", "r", stdin);
memset(d, 0, sizeof d);
scanf("%d", &n);
for (int i = 0; i < n-1; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
a++, b++;
g.add(a, b, c), g.add(b, a, c);
}
// dfs
dfs(1, 0);
// trie
for (int i = 1; i <= n; i++) trie.insert(d[i]);
int res = 0;
for (int i = 1; i <= n; i++) res = max(res, trie.query(d[i]));
printf("%d\n", res);
}