#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <array>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<ll, ll>
#define f first
#define s second
typedef array<int, 2> T;
T operator+(T a, T b){
return {a[0] + b[0], a[1] + b[1]};
}
const int maxn = 100000, k = 22;
int n;
int a[maxn];
T dp[maxn][k];
vector<int> graph[maxn];
ll dfs(int c, int p){
ll ret = a[c], cur = 0;
for(int i : graph[c]){
if(i == p) continue;
ret += dfs(i, c);
for(int j = 0; j < k; j++){
dp[c][j] = dp[c][j] + dp[i][j];
}
}
for(int i = 0; i < k; i++){
int x = (a[c] >> i) & 1;
if(x) swap(dp[c][i][0], dp[c][i][1]);
dp[c][i][x]++;
}
for(int i : graph[c]){
if(i == p) continue;
for(int j = 0; j < k; j++)
for(int t = 0; t < 2; t++){
int x = (a[c] >> j) & 1;
cur += (dp[i][j][t] * (dp[c][j][!t] - dp[i][j][!t ^ x] - (x == !t))) << j;
ret += dp[i][j][t] * (x == !t) << j;
}
}
ret += cur >> 1;
return ret;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < n - 1; i++){
int u, v;
cin >> u >> v;
u--, v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
cout << dfs(0, -1) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
4 |
Incorrect |
9 ms |
2944 KB |
Output isn't correct |
5 |
Incorrect |
7 ms |
2944 KB |
Output isn't correct |
6 |
Incorrect |
93 ms |
34428 KB |
Output isn't correct |
7 |
Incorrect |
95 ms |
34424 KB |
Output isn't correct |
8 |
Incorrect |
90 ms |
25464 KB |
Output isn't correct |
9 |
Incorrect |
92 ms |
24568 KB |
Output isn't correct |
10 |
Incorrect |
109 ms |
23672 KB |
Output isn't correct |