// NK
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
typedef pair<int,int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
#define pb push_back
#define eb emplace_back
#define pob pop_back
#define psf push_front
#define pof pop_front
#define mkp make_pair
#define mkt make_tuple
#define all(x) x.begin(), x.end()
#define Bolivia_is_nice ios::sync_with_stdio(false), cin.tie(nullptr)
//typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ord_set;
const int MAXS = 1e5+2;
const int LOG = 29;
vector<ll> ve(MAXS);
vector<int> g[MAXS];
ll dp[MAXS][LOG][2];
ll ans;
void dfs(int u, int p){
for (int i=0; i<g[u].size(); i++){
int v = g[u][i];
if (v!=p){
dfs(v, u);
for (int j=0; j<LOG; j++){
int ison = ((int)ve[u] >> j) & 1;
ll temp = dp[u][j][0] * dp[v][j][1] + dp[u][j][1] * dp[v][j][0];
ans += temp * (1 << j);
dp[u][j][1] += dp[v][j][ison^1];
ans += dp[v][j][ison^1] * (1<<j);
dp[u][j][0] += dp[v][j][ison];
}
}
}
for (int i=0; i<LOG; i++){
int ison = ((int)ve[u] >> i) & 1;
dp[u][i][ison]++;
}
}
void solve(){
int n; cin >> n;
for (int i=1; i<=n; i++){
cin >> ve[i];
ans += ve[i];
}
for (int i=1; i<n; i++){
int u,v; cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1,-1);
cout << ans << '\n';
}
int main(){
/*Bolivia_is_nice;
int t = 1; cin>>t;
while(t--)
solve();*/
Bolivia_is_nice;
solve();
return 0;
}
/*
~/.emacs
*/
Compilation message
deblo.cpp: In function 'void dfs(int, int)':
deblo.cpp:35:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i=0; i<g[u].size(); i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3456 KB |
Output is correct |
2 |
Correct |
2 ms |
3436 KB |
Output is correct |
3 |
Correct |
2 ms |
3564 KB |
Output is correct |
4 |
Correct |
3 ms |
3948 KB |
Output is correct |
5 |
Correct |
3 ms |
3948 KB |
Output is correct |
6 |
Correct |
105 ms |
65536 KB |
Output is correct |
7 |
Correct |
106 ms |
65536 KB |
Output is correct |
8 |
Correct |
102 ms |
56172 KB |
Output is correct |
9 |
Correct |
104 ms |
55276 KB |
Output is correct |
10 |
Correct |
131 ms |
53868 KB |
Output is correct |