Submission #318758

# Submission time Handle Problem Language Result Execution time Memory
318758 2020-11-03T06:49:50 Z shrek12357 Deblo (COCI18_deblo) C++14
45 / 90
165 ms 65540 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define ll long long
//cin.tie(0);ios_base::sync_with_stdio(0); 

const int MAXN = 1e5 + 5;
vector<ll> vals(MAXN);
vector<ll> adjList[MAXN];
ll n;
ll dp[MAXN][40][2];
ll ans[40];

void dfs(int src, int par) {
	for (int i = 0; i < 40; i++) {
		if ((vals[src] >> i) & 1) {
			ans[i]++;
			dp[src][i][1] = 1;
		}
		else {
			dp[src][i][0] = 1;
		}
	}
	for (auto i : adjList[src]) {
		if (i == par) {
			continue;
		}
		dfs(i, src);
		for (int j = 0; j < 40; j++) {
			ans[j] += dp[src][j][1] * dp[i][j][0];
			ans[j] += dp[src][j][0] * dp[i][j][1];
			if ((vals[src] >> j) & 1) {
				dp[src][j][1] += dp[i][j][0];
				dp[src][j][0] += dp[i][j][1];
			}
			else {
				dp[src][j][1] += dp[i][j][1];
				dp[src][j][0] += dp[i][j][0];
			}
		}
	}
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		ll temp;
		cin >> temp;
		vals[i] = temp;
	}
	for (int i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		adjList[a].push_back(b);
		adjList[b].push_back(a);
	}
	dfs(0, 0);
	ll ret = 0;
	for (int i = 0; i < 40; i++)
		ret += (ll)ans[i] * (1 << i);
	cout << ret << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3436 KB Output is correct
2 Correct 3 ms 3436 KB Output is correct
3 Correct 2 ms 3564 KB Output is correct
4 Correct 4 ms 4204 KB Output is correct
5 Correct 4 ms 4076 KB Output is correct
6 Runtime error 150 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
7 Runtime error 147 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
8 Runtime error 165 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
9 Runtime error 158 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
10 Runtime error 161 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)