Submission #790581

# Submission time Handle Problem Language Result Execution time Memory
790581 2023-07-22T21:43:25 Z skittles1412 Simurgh (IOI17_simurgh) C++17
51 / 100
263 ms 31252 KB
#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

int count_common_roads(const vector<int>& r);

template <typename T>
ostream& operator<<(ostream& out, const vector<T>& arr) {
    out << "[";
    for (int i = 0; i < sz(arr); i++) {
        if (i) {
            out << ", ";
        }
        out << arr[i];
    }
    out << "]";
    return out;
}

struct Q {
    map<vector<int>, int> cache;

    int query(vector<int> arr) {
        sort(begin(arr), end(arr));
        auto [it, inserted] = cache.emplace(arr, 0);
        if (inserted) {
            it->second = count_common_roads(arr);
        }
        return it->second;
    }
} Q;

struct DSU {
    vector<int> p;

    DSU(int n) : p(n, -1) {}

    int find(int u) {
        return p[u] < 0 ? u : (p[u] = find(p[u]));
    }

    bool merge(int u, int v) {
        u = find(u);
        v = find(v);
        if (u == v) {
            return false;
        }
        if (p[u] < p[v]) {
            swap(u, v);
        }
        p[v] += p[u];
        p[u] = v;
        return true;
    }
};

void to_comps(int n,
              int ign_u,
              const vector<int>& e_u,
              const vector<int>& e_v,
              vector<vector<int>>& out,
              vector<int>& st) {
    DSU dsu(n);
    for (int i = 0; i < sz(e_u); i++) {
        int u = e_u[i], v = e_v[i];
        if (u == ign_u || v == ign_u) {
            continue;
        }
        if (dsu.merge(u, v)) {
            st.push_back(i);
        }
    }
    out.resize(n);
    for (int i = 0; i < n; i++) {
        out[dsu.find(i)].push_back(i);
    }
    out.erase(remove_if(begin(out), end(out),
                        [&](const auto& c) -> bool {
                            return !sz(c) || (sz(c) == 1 && c[0] == ign_u);
                        }),
              out.end());
}

vector<int> find_roads(int n, vector<int> e_u, vector<int> e_v) {
    int m = sz(e_u);
    vector<pair<int, int>> graph[n];
    for (int i = 0; i < m; i++) {
        int u = e_u[i], v = e_v[i];
        graph[u].emplace_back(v, i);
        graph[v].emplace_back(u, i);
    }

    bool e_ans[m] {};
    vector<int> ans, st;
    vector<vector<int>> comps;

    for (int cu = 0; cu < n; cu++) {
        comps.clear();
        st.clear();

        auto decided = [&](int ei) -> bool {
            return e_u[ei] < cu || e_v[ei] < cu;
        };

        to_comps(n, cu, e_u, e_v, comps, st);
        int comp_ind[n];
        for (int i = 0; i < sz(comps); i++) {
            assert(sz(comps[i]));
            for (auto& a : comps[i]) {
                dbg(a);
                comp_ind[a] = i;
            }
            dbg("---");
        }
        vector<int> comp_e[sz(comps)];
        for (auto& [v, ei] : graph[cu]) {
            comp_e[comp_ind[v]].push_back(ei);
        }
        dbg(cu, sz(comps));

        for (int i = 0; i < sz(comps); i++) {
            auto cst = st;
            for (int j = 0; j < sz(comps); j++) {
                if (i == j) {
                    continue;
                }
                cst.push_back(comp_e[j][0]);
            }

            vector<int> to_decide;
            for (auto& a : comp_e[i]) {
                if (!decided(a)) {
                    to_decide.push_back(a);
                }
            }

            if (!sz(to_decide)) {
                continue;
            }

            int rep = -1;
            for (auto& a : comp_e[i]) {
                if (decided(a)) {
                    rep = e_ans[a];
                    to_decide.push_back(a);
                    break;
                }
            }

            if (sz(to_decide) == 1) {
                ans.push_back(to_decide[0]);
                continue;
            }

            vector<int> dqueries;
            for (auto& a : to_decide) {
                cst.push_back(a);
                dqueries.push_back(Q.query(cst));
                cst.pop_back();
            }

            int cmn = *min_element(begin(dqueries), end(dqueries)),
                cmx = *max_element(begin(dqueries), end(dqueries));

            if (cmn == cmx) {
                if (!rep) {
                    continue;
                }
            }
            dbg(to_decide, dqueries);
            for (int j = 0; j < sz(to_decide); j++) {
                if (dqueries[j] == cmx && !decided(to_decide[j])) {
                    dbg(to_decide[j]);
                    e_ans[to_decide[j]] = true;
                    ans.push_back(to_decide[j]);
                }
            }
        }
    }

    dbg(ans);

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 292 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 1 ms 300 KB correct
7 Correct 1 ms 300 KB correct
8 Correct 1 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 292 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 1 ms 300 KB correct
7 Correct 1 ms 300 KB correct
8 Correct 1 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 3 ms 596 KB correct
15 Correct 3 ms 672 KB correct
16 Correct 3 ms 596 KB correct
17 Correct 2 ms 596 KB correct
18 Correct 2 ms 428 KB correct
19 Correct 2 ms 596 KB correct
20 Correct 2 ms 596 KB correct
21 Correct 2 ms 596 KB correct
22 Correct 2 ms 468 KB correct
23 Correct 2 ms 424 KB correct
24 Correct 2 ms 516 KB correct
25 Correct 1 ms 304 KB correct
26 Correct 2 ms 424 KB correct
27 Correct 2 ms 468 KB correct
28 Correct 1 ms 300 KB correct
29 Correct 1 ms 340 KB correct
30 Correct 2 ms 468 KB correct
31 Correct 2 ms 468 KB correct
32 Correct 2 ms 468 KB correct
33 Correct 2 ms 428 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 292 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 1 ms 300 KB correct
7 Correct 1 ms 300 KB correct
8 Correct 1 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 3 ms 596 KB correct
15 Correct 3 ms 672 KB correct
16 Correct 3 ms 596 KB correct
17 Correct 2 ms 596 KB correct
18 Correct 2 ms 428 KB correct
19 Correct 2 ms 596 KB correct
20 Correct 2 ms 596 KB correct
21 Correct 2 ms 596 KB correct
22 Correct 2 ms 468 KB correct
23 Correct 2 ms 424 KB correct
24 Correct 2 ms 516 KB correct
25 Correct 1 ms 304 KB correct
26 Correct 2 ms 424 KB correct
27 Correct 2 ms 468 KB correct
28 Correct 1 ms 300 KB correct
29 Correct 1 ms 340 KB correct
30 Correct 2 ms 468 KB correct
31 Correct 2 ms 468 KB correct
32 Correct 2 ms 468 KB correct
33 Correct 2 ms 428 KB correct
34 Correct 251 ms 31152 KB correct
35 Correct 245 ms 30156 KB correct
36 Correct 159 ms 20964 KB correct
37 Correct 11 ms 1704 KB correct
38 Correct 243 ms 31000 KB correct
39 Correct 217 ms 26560 KB correct
40 Correct 162 ms 20916 KB correct
41 Correct 237 ms 31252 KB correct
42 Correct 263 ms 31132 KB correct
43 Correct 114 ms 16844 KB correct
44 Correct 99 ms 13536 KB correct
45 Correct 120 ms 15816 KB correct
46 Correct 90 ms 11868 KB correct
47 Correct 46 ms 5432 KB correct
48 Correct 6 ms 596 KB correct
49 Correct 12 ms 1876 KB correct
50 Correct 35 ms 5428 KB correct
51 Correct 110 ms 15772 KB correct
52 Correct 99 ms 13540 KB correct
53 Correct 92 ms 11860 KB correct
54 Correct 124 ms 16808 KB correct
55 Correct 113 ms 15896 KB correct
56 Correct 110 ms 15924 KB correct
57 Correct 96 ms 15872 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Incorrect 150 ms 23708 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 292 KB correct
5 Correct 1 ms 212 KB correct
6 Correct 1 ms 300 KB correct
7 Correct 1 ms 300 KB correct
8 Correct 1 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 3 ms 596 KB correct
15 Correct 3 ms 672 KB correct
16 Correct 3 ms 596 KB correct
17 Correct 2 ms 596 KB correct
18 Correct 2 ms 428 KB correct
19 Correct 2 ms 596 KB correct
20 Correct 2 ms 596 KB correct
21 Correct 2 ms 596 KB correct
22 Correct 2 ms 468 KB correct
23 Correct 2 ms 424 KB correct
24 Correct 2 ms 516 KB correct
25 Correct 1 ms 304 KB correct
26 Correct 2 ms 424 KB correct
27 Correct 2 ms 468 KB correct
28 Correct 1 ms 300 KB correct
29 Correct 1 ms 340 KB correct
30 Correct 2 ms 468 KB correct
31 Correct 2 ms 468 KB correct
32 Correct 2 ms 468 KB correct
33 Correct 2 ms 428 KB correct
34 Correct 251 ms 31152 KB correct
35 Correct 245 ms 30156 KB correct
36 Correct 159 ms 20964 KB correct
37 Correct 11 ms 1704 KB correct
38 Correct 243 ms 31000 KB correct
39 Correct 217 ms 26560 KB correct
40 Correct 162 ms 20916 KB correct
41 Correct 237 ms 31252 KB correct
42 Correct 263 ms 31132 KB correct
43 Correct 114 ms 16844 KB correct
44 Correct 99 ms 13536 KB correct
45 Correct 120 ms 15816 KB correct
46 Correct 90 ms 11868 KB correct
47 Correct 46 ms 5432 KB correct
48 Correct 6 ms 596 KB correct
49 Correct 12 ms 1876 KB correct
50 Correct 35 ms 5428 KB correct
51 Correct 110 ms 15772 KB correct
52 Correct 99 ms 13540 KB correct
53 Correct 92 ms 11860 KB correct
54 Correct 124 ms 16808 KB correct
55 Correct 113 ms 15896 KB correct
56 Correct 110 ms 15924 KB correct
57 Correct 96 ms 15872 KB correct
58 Correct 0 ms 212 KB correct
59 Correct 0 ms 212 KB correct
60 Incorrect 150 ms 23708 KB WA in grader: NO
61 Halted 0 ms 0 KB -