# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
890292 | pcc | Deblo (COCI18_deblo) | C++14 | 80 ms | 18680 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>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 1e5+10;
vector<int> tree[mxn];
int val[mxn],dp[mxn][2],arr[mxn];
int n;
ll ans = 0;
void dfs(int now,int par,int bit){
dp[now][0] = dp[now][1] = 0;
dp[now][(arr[now]&(1<<bit)?1:0)] = 1;
val[now] = (arr[now]&(1<<bit)?1:0);
for(auto nxt:tree[now]){
if(nxt == par)continue;
dfs(nxt,now,bit);
ans += 1ll*dp[now][1]*dp[nxt][0]*(1<<bit);
ans += 1ll*dp[now][0]*dp[nxt][1]*(1<<bit);
dp[now][val[now]] += dp[nxt][0];
dp[now][val[now]^1] += dp[nxt][1];
}
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i = 1;i<=n;i++)cin>>arr[i];
for(int i = 1;i<n;i++){
int a,b;
cin>>a>>b;
tree[a].push_back(b);
tree[b].push_back(a);
}
for(int i = 0;i<20;i++){
dfs(1,1,i);
}
for(int i = 1;i<=n;i++)ans += arr[i];
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |