Submission #522427

#TimeUsernameProblemLanguageResultExecution timeMemory
522427KoDDeblo (COCI18_deblo)C++17
90 / 90
144 ms12616 KiB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::tuple;
using std::pair;

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N;
    std::cin >> N;
    vector<int> A(N);
    for (auto& x : A) {
        std::cin >> x;
    }    
    vector<vector<int>> graph(N);
    for (int i = 1; i < N; ++i) {
        int a, b;
        std::cin >> a >> b;
        a -= 1, b -= 1;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    vector<int> parent(N), order;
    order.reserve(N);
    auto dfs = [&](auto&& self, const int u) -> void {
        order.push_back(u);
        for (const int v : graph[u]) {
            if (v != parent[u]) {
                parent[v] = u;
                self(self, v);
            }
        }
    };
    parent[0] = -1;
    dfs(dfs, 0);
    vector<int> accum = A;
    for (int i = 1; i < N; ++i) {
        accum[order[i]] ^= accum[parent[order[i]]];
    }
    long long ans = std::accumulate(A.begin(), A.end(), 0ll);
    for (int d = 0; d < 22; ++d) {
        long long cnt = 0;
        vector<array<int, 2>> count(N);
        for (int i = N - 1; i >= 0; --i) {
            const int u = order[i];
            const int a = A[u] >> d & 1, ac = accum[u] >> d & 1;
            for (const int v : graph[u]) {
                if (v != parent[u]) {
                    cnt += count[v][(a ^ ac ^ 1)];
                    for (int k = 0; k < 2; ++k) {
                        cnt += (long long)count[v][k] * count[u][a ^ k ^ 1];
                    }
                    for (int k = 0; k < 2; ++k) {
                        count[u][k] += count[v][k];
                    }
                }
            }
            count[u][ac] += 1;
        }
        ans += cnt << d;
    }
    std::cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...