#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
const int N = 100000 + 3;
int n, a[N], dp[N][23][2];
vector <vector <int> > v;
ll ans = 0;
void dfs(int x, int pr = -1){
for (int i = 0; i < 23; i++)
dp[x][i][(a[x]>>i)&1]++;
for (auto u: v[x]){
if (u == pr) continue;
dfs(u, x);
for (int i = 0; i < 23; i++){
ans += (dp[x][i][1] * dp[u][i][0] + dp[x][i][0] * dp[u][i][1]) * (1<<i);
if ((a[x]>>i) & 1){
dp[x][i][0] += dp[u][i][1];
dp[x][i][1] += dp[u][i][0];
}else{
dp[x][i][0] += dp[u][i][0];
dp[x][i][1] += dp[u][i][1];
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
cin >> n;
v.resize(n);
int sum = 0;
for (int i = 0; i < n; i++){
cin >> a[i];
sum += a[i];
}
for (int i = 0; i < n - 1; i++){
int x, y;
cin >> x >> y;
--x; --y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(0);
cout << ans + sum << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
6 |
Incorrect |
80 ms |
32504 KB |
Output isn't correct |
7 |
Incorrect |
75 ms |
32504 KB |
Output isn't correct |
8 |
Incorrect |
76 ms |
26104 KB |
Output isn't correct |
9 |
Incorrect |
76 ms |
25464 KB |
Output isn't correct |
10 |
Incorrect |
118 ms |
24952 KB |
Output isn't correct |