经典的字典树问题,我们知道查询任意两点的路径异或值为图片说明
所以,我们求出从根节点到每个节点的异或值,然后将这些值放到字典树上去进行查询就可以求得每个点的与其余点的最大路径异或权值了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define pii pair<int, int>
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N;
namespace Graph
{
    int head[maxN], cnt;
    struct Eddge
    {
        int nex, to, val;
        Eddge(int a=-1, int b=0, int c=0):nex(a), to(b), val(c) {}
    } edge[maxN << 1];
    inline void addEddge(int u, int v, int w)
    {
        edge[cnt] = Eddge(head[u], v, w);
        head[u] = cnt++;
    }
    inline void _add(int u, int v, int w) { addEddge(u, v, w); addEddge(v, u, w); }
    inline void init()
    {
        cnt = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
    }
};
using namespace Graph;
int dis[maxN];
void dfs(int u, int fa)
{
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(v == fa) continue;
        dis[v] = dis[u] ^ edge[i].val;
        dfs(v, u);
    }
}
struct node
{
    int nex[2];
    node() { memset(nex, 0, sizeof(nex)); }
} t[maxN * 32];
int tot = 1;
void Insert(int x)
{
    int rt = 1;
    for(int i=30, id; i>=0; i--)
    {
        id = (x >> i) & 1;
        if(!t[rt].nex[id])
        {
            t[rt].nex[id] = ++tot;
        }
        rt = t[rt].nex[id];
    }
}
int query(int x)
{
    int rt = 1, ans = 0;
    for(int i=30, id; i>=0; i--)
    {
        id = (x >> i) & 1;
        if(t[rt].nex[id ^ 1])
        {
            rt = t[rt].nex[id ^ 1];
            ans |= 1 << i;
        }
        else rt = t[rt].nex[id];
    }
    return ans;
}
int main()
{
    scanf("%d", &N);
    init();
    for(int i=1, u, v, w; i<N; i++)
    {
        scanf("%d%d%d", &u, &v, &w);
        _add(u, v, w);
    }
    dis[1] = 0;
    dfs(1, 0);
    int ans = 0;
    Insert(0);
    for(int i=1; i<=N; i++)
    {
        ans = max(ans, query(dis[i]));
        Insert(dis[i]);
    }
    printf("%d\n", ans);
    return 0;
}