#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<int> vals(MAXN);
vector<int> adjList[MAXN];
int n;
int dp[MAXN][40][2];
int 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);
int ret = 0;
for (int i = 0; i < n; i++) {
ret += pow(2, i) * ans[i];
}
cout << ret << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3072 KB |
Output is correct |
2 |
Correct |
2 ms |
3052 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3180 KB |
Output isn't correct |
4 |
Incorrect |
4 ms |
3436 KB |
Output isn't correct |
5 |
Incorrect |
4 ms |
3436 KB |
Output isn't correct |
6 |
Incorrect |
180 ms |
47224 KB |
Output isn't correct |
7 |
Incorrect |
176 ms |
47220 KB |
Output isn't correct |
8 |
Incorrect |
193 ms |
40928 KB |
Output isn't correct |
9 |
Incorrect |
178 ms |
40376 KB |
Output isn't correct |
10 |
Incorrect |
206 ms |
39520 KB |
Output isn't correct |