#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 1e5+10;
vector<int> tree[mxn];
int val[mxn],dp[mxn][2],arr[mxn];
int n;
ll ans = 0;
void dfs(int now,int par,int bit){
dp[now][0] = dp[now][1] = 0;
dp[now][(arr[now]&(1<<bit)?1:0)] = 1;
val[now] = (arr[now]&(1<<bit)?1:0);
for(auto nxt:tree[now]){
if(nxt == par)continue;
dfs(nxt,now,bit);
ans += 1ll*dp[now][1]*dp[nxt][0]*(1<<bit);
ans += 1ll*dp[now][0]*dp[nxt][1]*(1<<bit);
dp[now][val[now]] += dp[nxt][0];
dp[now][val[now]^1] += dp[nxt][1];
}
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i = 1;i<=n;i++)cin>>arr[i];
for(int i = 1;i<n;i++){
int a,b;
cin>>a>>b;
tree[a].push_back(b);
tree[b].push_back(a);
}
for(int i = 0;i<20;i++){
dfs(1,1,i);
}
for(int i = 1;i<=n;i++)ans += arr[i];
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4188 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
4188 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
4184 KB |
Output isn't correct |
6 |
Incorrect |
63 ms |
18680 KB |
Output isn't correct |
7 |
Correct |
59 ms |
18516 KB |
Output is correct |
8 |
Incorrect |
57 ms |
11112 KB |
Output isn't correct |
9 |
Incorrect |
60 ms |
10244 KB |
Output isn't correct |
10 |
Correct |
80 ms |
9296 KB |
Output is correct |