#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) {
ll 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];
}
}
int32_t 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 'int32_t 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 |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
1 ms |
2772 KB |
Output is correct |
3 |
Correct |
1 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
58 ms |
35420 KB |
Output is correct |
7 |
Correct |
57 ms |
35508 KB |
Output is correct |
8 |
Correct |
67 ms |
27784 KB |
Output is correct |
9 |
Correct |
63 ms |
26968 KB |
Output is correct |
10 |
Correct |
119 ms |
26252 KB |
Output is correct |