Submission #890292

#TimeUsernameProblemLanguageResultExecution timeMemory
890292pccDeblo (COCI18_deblo)C++14
36 / 90
80 ms18680 KiB
#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 timeMemoryGrader output
Fetching results...