题解
考虑一个点的贡献,如果过这个点的路径,有偶数条,则贡献为0,否则贡献为这个点的权值。最后所有点的贡献的异或和就是答案。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int n;
int cnt[N], a[N];
vector<int> G[N];
void addedge(int u, int v) {
G[u].push_back(v);
G[v].push_back(u);
}
int DFS(int u, int p) {
int deg = 0;
for (auto v : G[u]) if (v != p) {
int tp = DFS(v, u);
cnt[u] += tp;
cnt[u] += tp * deg;
deg += tp;
}
deg++;
cnt[u] += deg * (n - deg);
return deg;
}
int main()
{
cin >> n;
for (int i = 1, u, v; (i < n) && (scanf("%d %d", &u, &v)); ++i) addedge(u, v);
for (int i = 1; i <= n; ++i) cin >> a[i];
DFS(1, 0);
for (int i = 1, ans = 0; i <= n; ++i ) {
if (cnt[i] & 1) ans ^= a[i];
if (i == n) cout << ans;
}
return 0;
}