#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 100005
#define M ll(1e9 + 7)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef short int si;
vector <int> g[N];
ll dp[N][25][2], a[N], ans, st[25];
void dfs(int v, int p)
{
for (int i = 0; i < 25; i++)
{
if ((1 << i) & a[v]) dp[v][i][1]++;
else dp[v][i][0]++;
}
for (auto it : g[v])
{
if (it == p) continue;
dfs(it, v);
for (int i = 0; i < 25; i++)
{
ans += (dp[v][i][0] * dp[it][i][1] + dp[v][i][1] * dp[it][i][0]) * st[i];
if ((1 << i) & a[v])
{
dp[v][i][0] += dp[it][i][1];
dp[v][i][1] += dp[it][i][0];
}
else
{
dp[v][i][0] += dp[it][i][0];
dp[v][i][1] += dp[it][i][1];
}
}
}
}
int main()
{
//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
st[0] = 1;
for (int i = 1; i < 25; i++) st[i] = 2 * st[i - 1];
int n;
cin >> n;
for (int i = 0; i < n; i++) {cin >> a[i]; ans += a[i];}
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
a--; b--;
g[a].pb(b); g[b].pb(a);
}
dfs(0, -1);
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
7 ms |
2816 KB |
Output is correct |
4 |
Correct |
6 ms |
3200 KB |
Output is correct |
5 |
Correct |
7 ms |
3072 KB |
Output is correct |
6 |
Correct |
109 ms |
55136 KB |
Output is correct |
7 |
Correct |
112 ms |
55160 KB |
Output is correct |
8 |
Correct |
114 ms |
47420 KB |
Output is correct |
9 |
Correct |
107 ms |
46712 KB |
Output is correct |
10 |
Correct |
135 ms |
45816 KB |
Output is correct |