답안 #318756

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
318756 2020-11-03T06:45:22 Z shrek12357 Deblo (COCI18_deblo) C++14
18 / 90
167 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++) {
		int 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 < n; i++) {
		ret += pow(2, i) * ans[i];
	}
	cout << ret << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3436 KB Output is correct
2 Correct 2 ms 3436 KB Output is correct
3 Incorrect 3 ms 3564 KB Output isn't correct
4 Incorrect 4 ms 4204 KB Output isn't correct
5 Incorrect 4 ms 4076 KB Output isn't correct
6 Runtime error 152 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
7 Runtime error 149 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
8 Runtime error 153 ms 65536 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 167 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)