# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
703657 | alanl | Deblo (COCI18_deblo) | C++14 | 136 ms | 58624 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 f first
#define s second
#define rep(X, a,b) for(int X=a;X<b;++X)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define NL "\n"
using namespace std;
typedef pair<long long,long long> pll;
typedef pair<int,int> pii;
typedef long long ll;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; }
template<typename A> ostream& operator<<(ostream &os, const vector<A> &p){
for(const auto &a:p)
os << a << " ";
os << "\n";
return os;
}
ll val[100010], dp[100010][26][2], ans;
vector<int> adj[100010];
void dfs1(int v, int p){
for(auto a:adj[v]){
if(a!=p){
dfs1(a, v);
rep(i,0,26){
if(val[v]&(1<<i)) dp[v][i][1]+=dp[a][i][0], dp[v][i][0]+=dp[a][i][1];
else dp[v][i][1]+=dp[a][i][1], dp[v][i][0]+=dp[a][i][0];
}
}
}
rep(i,0,26){
if(val[v]&(1<<i)) dp[v][i][1]++;
else dp[v][i][0]++;
}
}
void dfs2(int v, int p){
rep(i,0,26) ans+=dp[v][i][1]*(1ll<<i);
for(auto a:adj[v]){
if(a!=p){
rep(i,0,26){
ll t1=dp[v][i][1], t0=dp[v][i][0];
if(val[v]&(1<<i)) t1-=dp[a][i][0], t0-=dp[a][i][1];
else t1-=dp[a][i][1], t0-=dp[a][i][0];
if(val[a]&(1<<i)) dp[a][i][1]+=t0, dp[a][i][0]+=t1;
else dp[a][i][1]+=t1, dp[a][i][0]+=t0;
}
dfs2(a, v);
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
ll sum=0;
rep(i,1,n+1) cin>>val[i], sum+=val[i];
rep(i,0,n-1){
int a, b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs1(1, 0);
dfs2(1, 0);
cout<<(ans-sum)/2+sum<<NL;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |