답안 #461244

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
461244 2021-08-09T15:29:05 Z grt Deblo (COCI18_deblo) C++17
18 / 90
286 ms 21364 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]) {
			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 Incorrect 2 ms 2636 KB Output isn't correct
2 Incorrect 2 ms 2672 KB Output isn't correct
3 Incorrect 2 ms 2636 KB Output isn't correct
4 Incorrect 3 ms 2764 KB Output isn't correct
5 Incorrect 3 ms 2636 KB Output isn't correct
6 Correct 160 ms 21364 KB Output is correct
7 Correct 153 ms 21188 KB Output is correct
8 Incorrect 139 ms 12480 KB Output isn't correct
9 Incorrect 147 ms 11472 KB Output isn't correct
10 Incorrect 286 ms 10308 KB Output isn't correct