#include <bits/stdc++.h>
using namespace std;
#define fo(i,s,t) for(int i = s; i <= t; ++ i)
#define fd(i,s,t) for(int i = s; i >= t; -- i)
#define bf(i,s) for(int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1ll<<60;
const db Inf = 1e20;
const db eps = 1e-9;
void gmax(int &a,int b){a = (a > b ? a : b);}
void gmin(int &a,int b){a = (a < b ? a : b);}
const int maxn = 100050;
int n, a[maxn], g[maxn][30][2];
ll ret[30][2], ans, sz[30][2];
VI adj[maxn];
void dfs(int u, int fa=0)
{
for(auto p : adj[u]) if(p != fa) dfs(p, u);
fo(j,0,29) ret[j][0] = ret[j][1] = sz[j][0] = sz[j][1] = 0;
fo(j,0,29) if(a[u]&(1<<j)) g[u][j][1] = 1; else g[u][j][0] = 1;
for(auto p : adj[u]) if(p != fa)
{
fo(j,0,29)
{
if(a[u]&(1<<j)) g[u][j][0] += g[p][j][1], g[u][j][1] += g[p][j][0];
else g[u][j][0] += g[p][j][0], g[u][j][1] += g[p][j][1];
ret[j][0] += sz[j][0] * g[p][j][0] + sz[j][1] * g[p][j][1];
ret[j][1] += sz[j][1] * g[p][j][0] + sz[j][0] * g[p][j][1];
sz[j][0] += g[p][j][0]; sz[j][1] += g[p][j][1];
}
}
fo(i,0,29) ret[i][0] += sz[i][0], ret[i][1] += sz[i][1];
fo(i,0,29) if(a[u]&(1<<i)) ans += (1<<i)*ret[i][0]; else ans += (1<<i)*ret[i][1];
ans += a[u];
// cout << u << ' ' << ans << endl;
}
int main()
{
#ifdef MPS
fp("1.in","r",stdin);
fp("1.out","w",stdout);
#endif
sf("%d",&n);
fo(i,1,n) sf("%d",&a[i]);
fo(i,2,n) {int u, v; sf("%d%d",&u,&v); adj[u].emplace_back(v); adj[v].emplace_back(u);}
dfs(1);
cout << ans << endl;
return 0;
}
Compilation message
deblo.cpp: In function 'int main()':
deblo.cpp:66:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
sf("%d",&n);
^
deblo.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fo(i,1,n) sf("%d",&a[i]);
^
deblo.cpp:68:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fo(i,2,n) {int u, v; sf("%d%d",&u,&v); adj[u].emplace_back(v); adj[v].emplace_back(u);}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2724 KB |
Output is correct |
3 |
Correct |
3 ms |
2752 KB |
Output is correct |
4 |
Correct |
5 ms |
3144 KB |
Output is correct |
5 |
Correct |
5 ms |
3144 KB |
Output is correct |
6 |
Correct |
124 ms |
39588 KB |
Output is correct |
7 |
Correct |
124 ms |
41468 KB |
Output is correct |
8 |
Correct |
128 ms |
41468 KB |
Output is correct |
9 |
Correct |
127 ms |
41468 KB |
Output is correct |
10 |
Correct |
172 ms |
41468 KB |
Output is correct |