#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
ll arr[maxn], value[maxn], n;
vector<int>g[maxn];
ll dp[maxn][2];
ll result, cnt;
void dfs(int node, int p) {
ll sum_even = 0LL, sum_odd = 0LL;
for(int i:g[node]) {
if(i != p) {
dfs(i, node);
if(value[node] == 0) {
dp[node][0] += dp[i][0];
dp[node][1] += dp[i][1];
}
else if(value[node] == 1){
dp[node][0] += dp[i][1];
dp[node][1] += dp[i][0];
}
sum_even += dp[i][0];
sum_odd += dp[i][1];
}
}
ll temp = 0LL;
if(g[node].size() == 1 && node != 1) {
if(value[node] == 1) {
dp[node][1] = 1LL;
}
else {
dp[node][0] = 1LL;
}
}
else {
if(value[node] == 0) {
for(int i:g[node]) {
if(i!=p) {
temp += 1LL * dp[i][0] * (sum_odd - dp[i][1]);
}
}
dp[node][0]++;
}
else if(value[node] == 1) {
ll tempb = 0LL;
for(int i:g[node]) {
if(i!=p) {
tempb += 1LL * dp[i][0] * (sum_even - dp[i][0]);
tempb += 1LL * dp[i][1] * (sum_odd - dp[i][1]);
}
}
tempb /= 2LL;
dp[node][1] ++;
temp += tempb;
}
}
temp += dp[node][1];
cnt += temp;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ifstream fin("input.txt");
fin>>n;
for(int i=1;i<=n;i++) {
fin>>arr[i];
}
int u, v;
for(int i=0;i<n-1;i++) {
fin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
for(int bit=0;bit<22;bit++) {
cnt = 0LL;
for(int i=1;i<=n;i++) {
if(arr[i]&(1<<bit)) {
value[i] = 1;
}
else {
value[i] = 0;
}
dp[i][0] = dp[i][1] = 0LL;
}
dfs(1, -1);
result += 1LL * cnt * (1LL<<bit);
}
cout<<result<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Incorrect |
4 ms |
2804 KB |
Output isn't correct |
3 |
Incorrect |
4 ms |
2804 KB |
Output isn't correct |
4 |
Incorrect |
4 ms |
2804 KB |
Output isn't correct |
5 |
Incorrect |
4 ms |
2912 KB |
Output isn't correct |
6 |
Incorrect |
4 ms |
2936 KB |
Output isn't correct |
7 |
Incorrect |
4 ms |
2936 KB |
Output isn't correct |
8 |
Incorrect |
4 ms |
2936 KB |
Output isn't correct |
9 |
Incorrect |
4 ms |
2936 KB |
Output isn't correct |
10 |
Incorrect |
4 ms |
2936 KB |
Output isn't correct |