답안 #461252

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
461252 2021-08-09T15:32:54 Z grt Deblo (COCI18_deblo) C++17
90 / 90
352 ms 17964 KB
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
#define _ ios_base::sync_with_stdio(0); cin.tie(0);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

const int nax = 100 * 1000 + 10;
int n;
int val[nax];
vi V[nax];
ll dp[nax], up[nax][2];

void dfs(int x, int p, int bit) {
	dp[x] = up[x][0] = up[x][1] = 0;
	for(int nbh : V[x]) if(nbh != p) {
		dfs(nbh, x, bit);
		dp[x] += dp[nbh];
		up[x][0] += up[nbh][0];
		up[x][1] += up[nbh][1];
	}
	if((1 << bit) & val[x]) {
		ll cur = 0;
		for(int nbh : V[x]) if(nbh != p) {
			cur += up[nbh][0] * (up[x][0] - up[nbh][0]);
			cur += up[nbh][1] * (up[x][1] - up[nbh][1]);
		}
		cur /= 2;
		dp[x] += cur;
		dp[x] += up[x][0];
		swap(up[x][0], up[x][1]);
		up[x][1]++;
		dp[x]++;
	} else {
		for(int nbh : V[x]) if(nbh != p) {
			dp[x] += up[nbh][0] * (up[x][1] - up[nbh][1]);
		}
		dp[x] += up[x][1];
		up[x][0]++;
	}
}

int main() {_
	cin >> n;
	for(int i = 1; i <= n; ++i) {
		cin >> val[i];
	}
	for(int a, b, i = 1; i < n; ++i) {
		cin >> a >> b;
		V[a].PB(b);
		V[b].PB(a);
	}
	ll ans = 0;
	for(int i = 0; i < 23; ++i) {
		dfs(1, 0, i);
		ans += (1LL << i) * dp[1];
	}
	cout << ans;
	
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 3 ms 2764 KB Output is correct
5 Correct 3 ms 2636 KB Output is correct
6 Correct 139 ms 17964 KB Output is correct
7 Correct 134 ms 17852 KB Output is correct
8 Correct 128 ms 10364 KB Output is correct
9 Correct 152 ms 9424 KB Output is correct
10 Correct 352 ms 8624 KB Output is correct