Submission #86314

# Submission time Handle Problem Language Result Execution time Memory
86314 2018-11-26T04:44:36 Z IvanC Deblo (COCI18_deblo) C++17
90 / 90
296 ms 15336 KB
#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

deblo.cpp: In function 'int main()':
deblo.cpp:74:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&N);
  ~~~~~^~~~~~~~~
deblo.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&valor[i]);
   ~~~~~^~~~~~~~~~~~~~~~
deblo.cpp:80:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&a,&b);
   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2684 KB Output is correct
3 Correct 4 ms 2744 KB Output is correct
4 Correct 6 ms 2792 KB Output is correct
5 Correct 8 ms 2792 KB Output is correct
6 Correct 185 ms 15336 KB Output is correct
7 Correct 158 ms 15336 KB Output is correct
8 Correct 196 ms 15336 KB Output is correct
9 Correct 208 ms 15336 KB Output is correct
10 Correct 296 ms 15336 KB Output is correct