Meaning of the questions:
As shown in the title, seeking (S (U_1, V_1) \) \ \ (\ Oplus \) \ (S (U_2, V_2) \) maximum.
analysis:
\ (1 \) violence Solution: Since \ (S (u, v) \) related to the ancestor of every point, so difficult to think of a \ (O (n ^ 2) \) method to calculate all \ (S (u, v) \) the value of its ancestors traversed violence against each vertex can be calculated. To count \ (S (U_1, V_1) \) \ (\ Oplus \) \ (S (U_2, V_2) \) maximum can be violent \ (O (p ^ {2 }) \) is calculated, wherein \ (P \) represents all duplicates removed \ (S (u, v) \) number. Then the total complexity is \ (O (p ^ 2) \) can not pass this question. \ (p \) the maximum value does not exceed \ (20,000 * 15 \) .
\ (2 \) I solution: weight of each vertex can be found in small, just use an array \ (vis [N] [16 ] \) labeled \ (U \) of ancestors to \ (U \) the exclusive oR of the node, when the \ (DFS \) traversing \ (U \) son \ (V \) , the re-calculation flag \ (V \) exclusive oR value. Thus to \ (O (n * 16) \) to obtain all \ (S (u, v) \) values. Then a \ (01 \) trie side insertion edge to query the maximum total complexity \ (O (the p-* log_2 (the p-)) \) .
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define pb push_back
const int N = 2e4 + 5, M = N * 16 + 5;
int n, u, v, ans, w[N];
bool val[N][17], flag[M];
int tot, ch[M * 19][2];
int head[N], cnt;
struct Graph {
int v, next;
} edge[N << 1];
void addedge(int u, int v) {
edge[++cnt].v = v;
edge[cnt].next = head[u];
head[u] = cnt;
}
void insert(int p) {
int u = 0, x;
for (int i = 19; ~i; i--) {
x = (p >> i) & 1;
if (!ch[u][x]) ch[u][x] = ++tot;
u = ch[u][x];
}
}
int query(int p) {
int u = 0, x, ans = 0;
for (int i = 19; ~i; i--) {
x = (p >> i) & 1;
if (ch[u][!x]) u = ch[u][!x], ans |= (1 << i);
else u = ch[u][x];
}
return ans;
}
void dfs(int u, int f) {
val[u][w[u]] = true;
if (!flag[w[u] * u]) {
flag[w[u] * u] = true;
insert(w[u] * u);
ans = max(ans, query(w[u] * u));
}
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
if (v == f) continue;
for (int j = 0; j <= 15; j++) {
if (val[u][j]) {
val[v][j ^ w[v]] = true;
if (!flag[(j ^ w[v]) * v]) {
flag[(j ^ w[v]) * v] = true;
insert((j ^ w[v]) * v);
ans = max(ans, query((j ^ w[v]) * v));
}
}
}
dfs(v, u);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
scanf("%d %d", &u, &v);
addedge(u, v), addedge(v, u);
}
for (int i = 1; i <= n; i++) scanf("%d", w + i);
dfs(1, 0);
printf("%d\n", ans);
return 0;
}