# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
86314 | IvanC | Deblo (COCI18_deblo) | C++17 | 296 ms | 15336 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;
typedef long long ll;
const int MAXN = 1e5 + 10;
const int MAXV = 3*1e6 + 10;
vector<int> grafo[MAXN];
int valor[MAXN],cor[MAXN],somatorio[MAXN][2],N;
ll xor_sum,even_sum;
void change(int v,int u,int sinal){
if(cor[v] == 1){
somatorio[v][0] += sinal*somatorio[u][1];
somatorio[v][1] += sinal*somatorio[u][0];
}
else{
somatorio[v][1] += sinal*somatorio[u][1];
somatorio[v][0] += sinal*somatorio[u][0];
}
}
void dfs1(int v,int p){
somatorio[v][0] = somatorio[v][1] = 0;
somatorio[v][cor[v]] = 1;
for(int u : grafo[v]){
if(u == p) continue;
dfs1(u,v);
change(v,u,1);
}
}
void dfs2(int v,int p){
even_sum += somatorio[v][1];
for(int u : grafo[v]){
if(u == p) continue;
int oldv0 = somatorio[v][0],oldv1 = somatorio[v][1]; // copia
// Removendo u de v
change(v,u,-1);
// Adicionando v a u
change(u,v,1);
dfs2(u,v);
// reverter
somatorio[v][0] = oldv0;
somatorio[v][1] = oldv1;
}
}
ll query_bit(int k){
for(int i = 1;i<=N;i++){
if(valor[i] & (1 << k)) cor[i] = 1;
else cor[i] = 0;
}
even_sum = 0;
dfs1(1,-1);
dfs2(1,-1);
return (1LL << k)*even_sum;
}
int main(){
scanf("%d",&N);
for(int i = 1;i<=N;i++){
scanf("%d",&valor[i]);
}
for(int i = 1;i<N;i++){
int a,b;
scanf("%d %d",&a,&b);
grafo[a].push_back(b);
grafo[b].push_back(a);
}
for(int i = 0;(1 << i) <= MAXV;i++) xor_sum += query_bit(i);
for(int i = 1;i<=N;i++) xor_sum -= valor[i];
xor_sum /= 2;
for(int i = 1;i<=N;i++) xor_sum += valor[i];
printf("%lld\n",xor_sum);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |