# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
197335 | quocnguyen1012 | Deblo (COCI18_deblo) | C++14 | 137 ms | 50424 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 mp make_pair
#define pb push_back
#define Task "A"
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
ll f[maxn][22][2];
int N, a[maxn];
vector<int> adj[maxn];
ll ans = 0;
bool testbit(int x, int n)
{
return x >> n & 1;
}
void dfs(int u, int p = -1)
{
for (int i=0; i<21; ++i){
f[u][i][testbit(a[u], i)] = 1;
}
for (int v : adj[u]){
if (v != p){
dfs(v, u);
for (int i=0; i<21; ++i){
ans += (1ll << i) * f[u][i][1] * f[v][i][0];
ans += (1ll << i) * f[u][i][0] * f[v][i][1];
if (testbit(a[u], i)){
f[u][i][0] += f[v][i][1];
f[u][i][1] += f[v][i][0];
}
else{
f[u][i][0] += f[v][i][0];
f[u][i][1] += f[v][i][1];
}
}
}
}
ans += a[u];
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(Task".INP", "r")){
freopen(Task".INP", "r", stdin);
freopen(Task".OUT", "w", stdout);
}
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].pb(v); adj[v].pb(u);
}
dfs(1);
cout << ans << '\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |