# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
707432 | nguyentunglam | Deblo (COCI18_deblo) | C++17 | 139 ms | 35368 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 10;
int a[N], f[2][26][N];
vector<int> adj[N];
long long res;
void dfs(int u, int par, int h) {
for(int bit = 0; bit <= 22; bit++) f[(h >> bit) & 1][bit][u]++;
res += a[u];
for(int v : adj[u]) if (v != par) {
dfs(v, u, h ^ a[v]);
for(int bit = 0; bit <= 22; bit++) for(int j = 0; j < 2; j++) for(int k = 0; k < 2; k++) {
bool x = (a[u] >> bit) & 1;
if (x ^ j ^ k) res += (1LL << bit) * f[j][bit][u] * f[k][bit][v];
}
for(int bit = 0; bit <= 22; bit++) for(int j = 0; j < 2; j++) f[j][bit][u] += f[j][bit][v];
}
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0, a[1]);
cout << res;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |