#include<bits/stdc++.h>
#define forin(i,a,b) for(int i=a;i<=b;++i)
#define ll long long
using namespace std;
const int N = 1e5 + 10;
int n;
ll ans;
ll val[N], d[N], bit[23][N], sz[N];
vector<int> adj[N];
void dfs(int u, int p = 0) {
sz[u] = 1;
forin(i,0,22) bit[i][u] += ((d[u] >> i) & 1);
for(auto v : adj[u]) if(v != p) {
d[v] ^= d[u];
dfs(v, u);
forin(i,0,22) {
int cost = 0;
if((val[u] >> i) & 1) {
cost += 1ll * bit[i][u] * bit[i][v] * (1 << i);
cost += 1ll * (sz[u] - bit[i][u]) * (sz[v] - bit[i][v]) * (1 << i);
}
else {
cost += 1ll * bit[i][u] * (sz[v] - bit[i][v]) * (1 << i);
cost += 1ll * (sz[u] - bit[i][u]) * bit[i][v] * (1 << i);
}
ans += cost;
}
forin(i,0,22) bit[i][u] += bit[i][v];
sz[u] += sz[v];
}
}
int main () {
cin.tie(0)->sync_with_stdio(0);
if(fopen("Task.inp","r")) {
freopen("Task.inp","r",stdin);
freopen("WA.out","w",stdout);
}
cin>>n;
forin(i,1,n) {
cin>>val[i];
d[i] = val[i];
}
forin(i,1,n - 1) {
int u, v; cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
forin(i,1,n) ans += val[i];
cout<<ans;
}
Compilation message
deblo.cpp: In function 'int main()':
deblo.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen("Task.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
deblo.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen("WA.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2772 KB |
Output isn't correct |
4 |
Incorrect |
3 ms |
3028 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
3028 KB |
Output isn't correct |
6 |
Incorrect |
64 ms |
37372 KB |
Output isn't correct |
7 |
Incorrect |
58 ms |
37324 KB |
Output isn't correct |
8 |
Incorrect |
64 ms |
29748 KB |
Output isn't correct |
9 |
Incorrect |
73 ms |
28896 KB |
Output isn't correct |
10 |
Incorrect |
110 ms |
28032 KB |
Output isn't correct |