#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][v][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 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
2 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
2816 KB |
Output isn't correct |
4 |
Incorrect |
7 ms |
3200 KB |
Output isn't correct |
5 |
Incorrect |
7 ms |
3200 KB |
Output isn't correct |
6 |
Incorrect |
114 ms |
56952 KB |
Output isn't correct |
7 |
Incorrect |
109 ms |
56952 KB |
Output isn't correct |
8 |
Incorrect |
119 ms |
49400 KB |
Output isn't correct |
9 |
Incorrect |
118 ms |
48504 KB |
Output isn't correct |
10 |
Incorrect |
138 ms |
47736 KB |
Output isn't correct |