#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _34raRxL ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
void print(){
cout<<"\n";
}
template<class te,class ...ti>
void print(const te&v, const ti&...nv) {
cout<<v;
if(sizeof...(nv)){
cout<<" ";
print(nv...);
}
}
using tpii=pair<int,pair<int,int>>;
using pii=pair<int,int>;
using vi=vector<int>;
using vll=vector<long long>;
void slv(){
int n;
cin>>n;
vi a(n);
rep(i,n){
cin>>a[i];
}
vec(vi) adj(n);
rep(i,n-1){
int u,v;
cin>>u>>v;
u--,v--;
adj[u].pb(v);
adj[v].pb(u);
}
vec(vec(vi)) cnt(n,vec(vi)(26,vi(2)));
vi pxor(n,-1);
ll ans=0;
auto dfs=[&](auto self,int v,int par)->void{
if(par!=-1){
pxor[v]=pxor[par];
}
int x=a[v];
ans+=x;
pxor[v]^=x;
for(auto u : adj[v]){
if(u!=par){
self(self,u,v);
}
}
rep(j,25){
for(auto u : adj[v]){
if(u==par){
continue;
}
if(x&(1<<j)){
// [0,0],[1,1]
ans+=(1<<j)*cnt[u][j][0]*cnt[v][j][0];
ans+=(1<<j)*cnt[u][j][1]*cnt[v][j][1];
}else{
// [0,1],[1,0]
ans+=(1<<j)*cnt[u][j][0]*cnt[v][j][1];
ans+=(1<<j)*cnt[u][j][1]*cnt[v][j][0];
}
if((pxor[v]^x)&(1<<j)){
// if(v==0 and u==1){
// print(pxor[x],"\n");
// }
ans+=(1<<j)*cnt[u][j][0];
}else{
ans+=(1<<j)*cnt[u][j][1];
}
cnt[v][j][0]+=cnt[u][j][0];
cnt[v][j][1]+=cnt[u][j][1];
}
cnt[v][j][bool(pxor[v]&(1<<j))]++;
}
};
dfs(dfs,0,-1);
print(ans,"\n");
}
int main(){
_34raRxL;
int t=1;
// cin>>t;
slv();
//
return 0;
}
Compilation message
deblo.cpp: In function 'int main()':
deblo.cpp:97:6: warning: unused variable 't' [-Wunused-variable]
97 | int t=1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
1868 KB |
Output isn't correct |
5 |
Incorrect |
3 ms |
1740 KB |
Output isn't correct |
6 |
Runtime error |
103 ms |
65540 KB |
Execution killed with signal 9 |
7 |
Runtime error |
84 ms |
65540 KB |
Execution killed with signal 9 |
8 |
Runtime error |
83 ms |
65540 KB |
Execution killed with signal 9 |
9 |
Runtime error |
103 ms |
65540 KB |
Execution killed with signal 9 |
10 |
Runtime error |
110 ms |
65540 KB |
Execution killed with signal 9 |