#include <bits/stdc++.h>
using namespace std;
#define flag long long
#define int flag
inline flag in(){flag x=0;char c=getchar();bool neg=false;while(c!='-'&&('0'>c||c>'9')) c=getchar();if(c=='-') neg=true,c=getchar();while('0'<=c&&c<='9') x=10*x+c-'0',c=getchar();if(neg) x=-x;return x;}
inline void out(flag x){if(x<0) putchar('-'),x=-x;if(x>9) out(x/10);putchar(x%10+'0');}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define task "396E"
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1<<(i-1)))
#define _ putchar(' ')
#define __ putchar('\n')
const int N=100010;
int n,s,r;
flag kq;
int c[N],a[N],dd[N];
int f[25][2],g[25][2];
vector<int> ad[N];
void pre(int u,int p)
{
s++;c[u]=1;
forv(v,ad[u]) if(v!=p && !dd[v]) pre(v,u),c[u]+=c[v];
}
int ftr(int u,int p)
{
forv(v,ad[u]) if(v!=p && !dd[v] && c[v]>s/2) return ftr(v,u);
return u;
}
void dfs(int u,int p,int c)
{
if(u!=p) forinc(i,1,22) f[i][bit(c,i)]++;
forv(v,ad[u]) if(v!=p && !dd[v])
{
dfs(v,u,c^a[v]);
if(u==p)
{
forinc(i,1,22) kq+=(f[i][1]*g[i][bit(a[p],i)] + f[i][0]*g[i][!bit(a[p],i)])*(1<<(i-1));
forinc(i,1,22) g[i][0]+=f[i][0],
g[i][1]+=f[i][1],
f[i][0]=f[i][1]=0;
}
}
}
void ctr(int u,int p,int o)
{
s=0;pre(u,u);
r=ftr(u,u);dd[r]=1;
forinc(i,1,22) g[i][0]=1,g[i][1]=f[i][0]=f[i][1]=0;
dfs(r,r,0);
forv(v,ad[r]) if(!dd[v]) ctr(v,r,o+1);
}
main()
{
//freopen(task".inp","r",stdin);
//freopen(task".out","w",stdout);
n=in();
forinc(i,1,n) a[i]=in(),kq+=a[i];
forinc(i,2,n)
{
int u=in(),v=in();
ad[u].pb(v);
ad[v].pb(u);
}
ctr(1,1,0);
cout<<kq;
}
Compilation message
deblo.cpp:67:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2808 KB |
Output is correct |
4 |
Correct |
8 ms |
2808 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
125 ms |
14712 KB |
Output is correct |
7 |
Correct |
125 ms |
14712 KB |
Output is correct |
8 |
Correct |
129 ms |
11384 KB |
Output is correct |
9 |
Correct |
131 ms |
11000 KB |
Output is correct |
10 |
Correct |
145 ms |
10624 KB |
Output is correct |