## Hint

$$1~\leq~n~\leq~10^5~,~1~\leq~w~<~2^{31}$$，其中 $$w$$ 是最大边权

## Code

#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define ci const int
#define cl const long long

typedef long long int ll;

namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}

template <typename T>
inline void qr(T &x) {
char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}

template <typename T>
char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
}

namespace OPT {
char buf[120];
}

template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
int top=0;
do {OPT::buf[++top] = char(x % 10 + '0');} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}

const int maxn = 100010;
const int maxm = 200010;
const int maxt = 6200010;

struct Edge {
Edge *nxt;
int to, v;
};
Edge eg[maxm], *hd[maxn]; int ecnt;
inline void cont(ci from, ci to, ci v) {
Edge &e = eg[++ecnt];
e.to = to; e.nxt = hd[from]; e.v = v; hd[from] = &e;
}

struct Tree {
Tree *son[2];
};
Tree qwq[maxt], *rot;
int top;

int n, ans;
int dist[maxn];

void dfs(ci, ci);
void buildroot();
void build(Tree*, ci, ci);
int check(Tree*, ci, ci);

int main() {
freopen("1.in", "r", stdin);
qr(n);
dfs(1, 0);
buildroot();
for (int i = 1; i <= n; ++i) build(rot, dist[i], 31);
for (int i = 1; i <= n; ++i) ans = std::max(ans, check(rot, dist[i], 31) ^ dist[i]);
qw(ans, '\n', true);
return 0;
}

int a, b, c;
for (int i = 1; i < n; ++i) {
a = b = c = 0; qr(a); qr(b); qr(c);
cont(a, b, c); cont(b, a, c);
}
}

void dfs(ci u, ci fa) {
for (Edge *e = hd[u]; e; e = e->nxt) {
int &to = e->to;
if (to == fa) continue;
dist[to] = dist[u] ^ e->v; dfs(to, u);
}
}

void buildroot() {
rot = qwq; top = 1;
}

void build(Tree *u, ci v, ci cur) {
if (cur < 0) return;
int k = static_cast<bool>((1 << cur) & v);
build(u->son[k] ? u->son[k] : u->son[k] = qwq + (top++), v, cur - 1);
}

int check(Tree *u, ci v, ci cur) {
if (cur < 0) return 0;
int k = (static_cast<bool>((1 << cur) & v)) ^ 1;
return u->son[k] ? (check(u->son[k], v, cur - 1) | (k << cur)) : check(u->son[k ^ 1], v, cur - 1) | ((k ^ 1) << cur);
}

0条评论