求树上的一条最长异或路径。
定义f(u, v)为u到v的路径, 那么显然f(1, u)^f(1, v) = f(u, v), 想不到这个就没有办法做。
然后就可以用字典树查询+插入了。
用指针版本的狂T不止。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e5+;
int head[maxn*], num, val[maxn], top = , cnt;
struct node
{
int to, nextt, w;
}e[maxn*];
void add(int u, int v, int w) {
e[num].to = v, e[num].w = w, e[num].nextt = head[u], head[u] = num++;
}
void init() {
num = cnt = ;
mem(val);
mem1(head);
}
void dfs(int u, int fa) {
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
val[v] = val[u]^e[i].w;
dfs(v, u);
}
}
struct Trie
{
int next[];
void init() {
next[] = next[] = ;
}
}trie[maxn];
void insert(int pre) {
int p = ;
for(int i = top-; i>=; i--) {
int tmp = pre>>i&;
if(!trie[p].next[tmp]) {
trie[p].next[tmp] = ++cnt;
trie[cnt].init();
}
p = trie[p].next[tmp];
}
}
int query(int pre) {
int p = , ret = ;
for(int i = top-; i>=; i--) {
int tmp = pre>>i&;
if(trie[p].next[tmp^]) {
ret |= <<i;
tmp ^= ;
}
p = trie[p].next[tmp];
}
return ret;
}
int main()
{
int n, x, y, z;
while(~scanf("%d", &n)) {
init();
for(int i = ; i<n-; i++) {
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
add(y, x, z);
}
val[] = ;
dfs(, -);
int ans = ;
trie[].init();
for(int i = ; i<n; i++) {
insert(val[i]);
ans = max(ans, query(val[i]));
}
printf("%d\n", ans);
}
return ;
}