//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define int long long
//#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()
using namespace std;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 2e5 + 5;
const int oo = 1e9 + 1;
const int mod = 1e9 + 7;
//const ll oo = 5e18;
int n, a[N], del[N], cnt[5][35], d[N], ans;
vector<int> p[N];
void cal(int u,int v){
d[u] = 1;
for(auto j : p[u]){
if(del[j] || j == v) continue;
cal(j, u);
d[u] += d[j];
}
}
int FIND(int u,int v,int root){
for(auto j : p[u]){
if(j == v || del[j]) continue;
if(d[j] > d[root] / 2) return FIND(j, u, root);
}
return u;
}
vector<int> vr;
void visit(int u,int v,int w){
ans += w;
for(int i = 22; i >= 0; i --){
int msk = (w >> i) & 1;
ans += 1ll * cnt[1 - msk][i] * (1ll << i);
}
vr.pb(w);
for(auto j : p[u]){
if(j == v || del[j]) continue;
visit(j, u, (w ^ a[j]));
}
}
void solve(int u){
cal(u, 0);
int x = FIND(u, 0, u);
del[x] = 1;
ans += a[x];
for(int i = 0; i <= 22; i ++) cnt[0][i] = cnt[1][i] = 0;
for(auto j : p[x]) if(!del[j]){
vr.clear();
visit(j, x, (a[x] ^ a[j]));
for(auto i : vr){
int u = (i ^ a[x]);
for(int p = 0; p <= 22; p ++){
int tmp = (u >> p) & 1;
cnt[tmp][p]++;
}
}
}
for(auto j : p[x]) if(!del[j]) solve(j);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
for(int i = 1; i < n; i ++){
int x, y; cin >> x >> y;
p[x].pb(y);
p[y].pb(x);
}
solve(1);
cout << ans;
}
Compilation message
deblo.cpp: In function 'int main()':
deblo.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
deblo.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8792 KB |
Output is correct |
2 |
Correct |
3 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8804 KB |
Output is correct |
4 |
Correct |
3 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
126 ms |
20944 KB |
Output is correct |
7 |
Correct |
127 ms |
20684 KB |
Output is correct |
8 |
Correct |
124 ms |
16252 KB |
Output is correct |
9 |
Correct |
127 ms |
15744 KB |
Output is correct |
10 |
Correct |
121 ms |
15416 KB |
Output is correct |