# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
707750 | ThMinh_ | Deblo (COCI18_deblo) | C++14 | 91 ms | 35508 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 forin(i,a,b) for(int i=a;i<=b;++i)
#define ll long long
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n;
ll ans;
ll val[N], d[N], bit[23][N], sz[N];
vector<int> adj[N];
void dfs(int u, int p = 0) {
sz[u] = 1;
forin(i,0,22) bit[i][u] += ((d[u] >> i) & 1);
for(auto v : adj[u]) if(v != p) {
d[v] ^= d[u];
dfs(v, u);
forin(i,0,22) {
int cost = 0;
if((val[u] >> i) & 1) {
cost += 1ll * bit[i][u] * bit[i][v] * (1 << i);
cost += 1ll * (sz[u] - bit[i][u]) * (sz[v] - bit[i][v]) * (1 << i);
}
else {
cost += 1ll * bit[i][u] * (sz[v] - bit[i][v]) * (1 << i);
cost += 1ll * (sz[u] - bit[i][u]) * bit[i][v] * (1 << i);
}
ans += cost;
}
forin(i,0,22) bit[i][u] += bit[i][v];
sz[u] += sz[v];
}
}
int32_t main () {
cin.tie(0)->sync_with_stdio(0);
if(fopen("Task.inp","r")) {
freopen("Task.inp","r",stdin);
freopen("WA.out","w",stdout);
}
cin>>n;
forin(i,1,n) {
cin>>val[i];
d[i] = val[i];
}
forin(i,1,n - 1) {
int u, v; cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
forin(i,1,n) ans += val[i];
cout<<ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |