Submission #255118

# Submission time Handle Problem Language Result Execution time Memory
255118 2020-07-31T09:54:37 Z Vladikus004 Deblo (COCI18_deblo) C++14
90 / 90
118 ms 51960 KB
#include <bits/stdc++.h>
#define inf 2e9
#define int long long
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

const int N = 100000 + 3;
int n, a[N], dp[N][23][2];
vector <vector <int> > v;

ll ans = 0;

void dfs(int x, int pr = -1){
    for (int i = 0; i < 23; i++)
        dp[x][i][(a[x]>>i)&1]++;
    for (auto u: v[x]){
        if (u == pr) continue;
        dfs(u, x);
        for (int i = 0; i < 23; i++){
            ans += (dp[x][i][1] * dp[u][i][0] + dp[x][i][0] * dp[u][i][1]) * (1<<i);
            if ((a[x]>>i) & 1){
                dp[x][i][0] += dp[u][i][1];
                dp[x][i][1] += dp[u][i][0];
            }else{
                dp[x][i][0] += dp[u][i][0];
                dp[x][i][1] += dp[u][i][1];
            }
        }
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
    #endif // LOCAL
    cin >> n;
    v.resize(n);
    int sum = 0;
    for (int i = 0; i < n; i++){
        cin >> a[i];
        sum += a[i];
    }
    for (int i = 0; i < n - 1; i++){
        int x, y;
        cin >> x >> y;
        --x; --y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0);
    cout << ans + sum << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 768 KB Output is correct
5 Correct 1 ms 768 KB Output is correct
6 Correct 86 ms 51960 KB Output is correct
7 Correct 86 ms 51960 KB Output is correct
8 Correct 85 ms 44768 KB Output is correct
9 Correct 92 ms 44024 KB Output is correct
10 Correct 118 ms 43256 KB Output is correct