# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
342268 | Hazem | Deblo (COCI18_deblo) | C++14 | 140 ms | 37996 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.
/*
ID: tmhazem1
LANG: C++14
TASK: pprime
*/
#include <bits/stdc++.h>
using namespace std;
#define S second
#define F first
#define LL long long
const int N = 2e5+10;
LL LINF = 100000000000000000;
LL INF = 1000000000;
int MOD = 1e9+7;
int vals[N],par[N][30],sizes[N];
LL pr[N],ans;
vector<int>adj[N];
vector<int>freq[N];
void dfs(int i,int pre){
sizes[i] = 1;
freq[i] = vector<int>(30,0);
pr[i] = pr[pre]^vals[i];
for(int j=0;j<30;j++)
freq[i][j] += (pr[i]&(1<<j))!=0;
for(auto x:adj[i]){
if(x==pre)continue;
dfs(x,i);
sizes[i] += sizes[x];
for(int j=0;j<30;j++)
freq[i][j] += freq[x][j];
}
for(auto x:adj[i]){
if(x==pre)continue;
sizes[i] -= sizes[x];
for(int j=0;j<30;j++){
freq[i][j] -= freq[x][j];
if(vals[i]&(1<<j))freq[x][j] = sizes[x]-freq[x][j];
ans += 1ll*(sizes[x]-freq[x][j])*(freq[i][j])*(1<<j)+1ll*freq[x][j]*(sizes[i]-freq[i][j])*(1<<j);
if(vals[i]&(1<<j))freq[x][j] = sizes[x]-freq[x][j];
freq[i][j] += freq[x][j];
}
}
for(auto x:adj[i])
if(x==pre)continue;
else
sizes[i] += sizes[x];
return ;
}
int main()
{
// freopen("out.txt","w",stdout);
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&vals[i]),ans += vals[i];
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1,1);
printf("%lld\n",ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |