Submission #758589

# Submission time Handle Problem Language Result Execution time Memory
758589 2023-06-14T23:43:41 Z DmitryKh Deblo (COCI18_deblo) C++14
45 / 90
1000 ms 51568 KB
#include <iostream>
#include <vector>
#include <list>
#include <unordered_map>

using namespace std;
using ll = long long;

void DFS(int v,
          const vector<vector<int>>& E,
          const vector<int>& A,
          vector<bool>& V,
          vector<ll>& S,
          vector<ll>& W) {
    S[v] = A[v];
    W.push_back(A[v]);
    V[v] = true;
    unordered_map<int, vector<ll>> TW;
    for (auto u : E[v]) {
        if (!V[u]) {
            DFS(u, E, A, V, S, TW[u]);
            S[v] += S[u];
            for (auto tw : TW[u]) {
                S[v] += A[v]^tw;
                W.push_back(A[v]^tw);
            }
        }
    }

    //cout << "S[" << v << "]=" << S[v] << '\n';

    list<int> SS;
    for (const auto& p : TW) {
        if (!p.second.empty()) {
            SS.push_back(p.first);
        }
    }

    while (!SS.empty()) {
        int u = SS.back();
        SS.pop_back();
        const auto& V = TW[u];
        for (auto e : SS) {
            const auto& E = TW[e];
            //cout << v << ": ";
            for (auto l : V)  {
                for (auto r : E) {
                    //cout << '\t' << A[v] << '^' << l << '^' << r << '\n';
                    S[v] += A[v]^l^r;
                }
            }
        }
    }
}

int main(int argc, char* argv[]) {
    int n;
    cin >> n;
    vector<int> A(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> A[i];
    }

    vector<vector<int>> E(n + 1);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        E[u].push_back(v);
        E[v].push_back(u);
    }

    {
        vector<ll> S(n + 1);
        vector<ll> F2(n + 1);
        {
            vector<ll> TW;
            vector<bool> V(n + 1);
            DFS(1, E, A, V, S, TW);
        }

        /*for (int i = 1; i <= n; i++)
            cout << "S[" << i << "]=" << S[i] << '\n';*/

        cout << S[1] << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Execution timed out 1084 ms 51512 KB Time limit exceeded
7 Execution timed out 1069 ms 51568 KB Time limit exceeded
8 Execution timed out 1072 ms 17864 KB Time limit exceeded
9 Execution timed out 1081 ms 14036 KB Time limit exceeded
10 Execution timed out 1086 ms 9860 KB Time limit exceeded