Submission #228946

#TimeUsernameProblemLanguageResultExecution timeMemory
228946VEGAnnDeblo (COCI18_deblo)C++14
90 / 90
191 ms31224 KiB
#include <bits/stdc++.h> #define PB push_back using namespace std; typedef long long ll; const int N = 100100; const int PW = 22; ll ans = 0; vector<int> g[N]; int n, vl[N], kol[2][PW][N]; void dfs(int v, int p){ ans += vl[v]; for (int po = 0; po < PW; po++) kol[bool(vl[v] & (1 << po))][po][v]++; for (int u : g[v]){ if (u == p) continue; dfs(u, v); for (int po = 0; po < PW; po++){ ans += (1ll << po) * kol[0][po][v] * kol[1][po][u]; ans += (1ll << po) * kol[1][po][v] * kol[0][po][u]; int bt = bool(vl[v] & (1 << po)); kol[0][po][v] += kol[bt][po][u]; kol[1][po][v] += kol[bt ^ 1][po][u]; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> vl[i]; for (int i = 1; i < n; i++){ int x, y; cin >> x >> y; x--; y--; g[x].PB(y); g[y].PB(x); } dfs(0, -1); cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...