# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
461252 | grt | Deblo (COCI18_deblo) | C++17 | 352 ms | 17964 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 PB push_back
#define ST first
#define ND second
#define _ ios_base::sync_with_stdio(0); cin.tie(0);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 100 * 1000 + 10;
int n;
int val[nax];
vi V[nax];
ll dp[nax], up[nax][2];
void dfs(int x, int p, int bit) {
dp[x] = up[x][0] = up[x][1] = 0;
for(int nbh : V[x]) if(nbh != p) {
dfs(nbh, x, bit);
dp[x] += dp[nbh];
up[x][0] += up[nbh][0];
up[x][1] += up[nbh][1];
}
if((1 << bit) & val[x]) {
ll cur = 0;
for(int nbh : V[x]) if(nbh != p) {
cur += up[nbh][0] * (up[x][0] - up[nbh][0]);
cur += up[nbh][1] * (up[x][1] - up[nbh][1]);
}
cur /= 2;
dp[x] += cur;
dp[x] += up[x][0];
swap(up[x][0], up[x][1]);
up[x][1]++;
dp[x]++;
} else {
for(int nbh : V[x]) if(nbh != p) {
dp[x] += up[nbh][0] * (up[x][1] - up[nbh][1]);
}
dp[x] += up[x][1];
up[x][0]++;
}
}
int main() {_
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> val[i];
}
for(int a, b, i = 1; i < n; ++i) {
cin >> a >> b;
V[a].PB(b);
V[b].PB(a);
}
ll ans = 0;
for(int i = 0; i < 23; ++i) {
dfs(1, 0, i);
ans += (1LL << i) * dp[1];
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |