Submission #140644

# Submission time Handle Problem Language Result Execution time Memory
140644 2019-08-04T03:04:30 Z tri Simurgh (IOI17_simurgh) C++14
100 / 100
620 ms 7544 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

namespace debug {
    const int DEBUG = false;

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x);

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x);

    template<class T>
    void pr(const vector<T> &x);

    template<class T>
    void pr(const set<T> &x);

    template<class T1, class T2>
    void pr(const map<T1, T2> &x);

    template<class T>
    void pr(const T &x) { if (DEBUG) cout << x; }

    template<class T, class... Ts>
    void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }

    template<class T>
    void prIn(const T &x) {
        pr("{");
        bool fst = 1;
        for (auto &a : x) {
            pr(fst ? "" : ", ", a), fst = 0;
        }
        pr("}");
    }

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x) { prIn(x); }

    template<class T>
    void pr(const vector<T> &x) { prIn(x); }

    template<class T>
    void pr(const set<T> &x) { prIn(x); }

    template<class T1, class T2>
    void pr(const map<T1, T2> &x) { prIn(x); }

    void ps() { pr("\n"), cout << flush; }

    template<class Arg, class... Args>
    void ps(const Arg &first, const Args &... rest) {
        pr(first, " ");
        ps(rest...);
    }
}
using namespace debug;

int count_common_roads(const vi &roads);

int N, M;
vi u, v, anc, tr, inTr, spec;

void dsu_init() {
    anc.resize(N);
    for (int i = 0; i < N; i++) {
        anc[i] = i;
    }
}

int fRt(int x) {
    return anc[x] == x ? x : anc[x] = fRt(anc[x]);
}

bool merge(int a, int b) {
    a = fRt(a), b = fRt(b);
    if (a == b) {
        return false;
    }
    anc[a] = b;
    return true;
}

vector<vector<pi>> trA, aL;

int goal;
vi path;

bool dfs(int cV, int pV) {
    if (cV == goal) return true;
    for (pi adj : trA[cV]) {
        if (adj.f == pV) continue;
        if (dfs(adj.f, cV)) {
            path.pb(adj.s);
            return true;
        }
    }
    return false;
}

int query(vi &base) {
    dsu_init();
    vi used;

    for (int x : base) {
        used.pb(x);
        assert(merge(u[x], v[x]));
    }

    for (int x : tr) {
        if (merge(u[x], v[x])) {
            used.pb(x);
        }
    }
    assert(used.size() == N - 1);
    return count_common_roads(used);
}

int queryForest(vi &base) {
    dsu_init();
    vi used;

    int oCnt = 0;
    for (int x : base) {
        used.pb(x);
        assert(merge(u[x], v[x]));
    }

    for (int x : tr) {
        if (merge(u[x], v[x]) && spec[x] != -1) {
            used.pb(x);
            oCnt += spec[x];
        }
    }
    assert(used.size() == N - 1);
    return count_common_roads(used) - oCnt;
}

void calcTree() {
    for (int ext = 0; ext < M; ext++) {
        if (inTr[ext]) continue;

        goal = v[ext];
        path.clear();
        dfs(u[ext], -1);

        vi k, uk;
        for (int x : path) {
            if (spec[x] == -1) {
                uk.pb(x);
            } else {
                k.pb(x);
            }
        }

        if (uk.empty()) continue;

        if (k.size()) { // if any are known
            vi base; // query cycle without a known
            base.pb(ext);
            for (int x : path) {
                if (x != k[0]) {
                    base.pb(x);
                }
            }

            int bCnt = query(base);

            for (int exc : uk) { // query cycle without unknowns
                base.clear();
                base.pb(ext);
                for (int inc : path) {
                    if (inc != exc) {
                        base.pb(inc);
                    }
                }

                int cCnt = query(base);
                if (cCnt < bCnt) {
                    spec[exc] = 1;
                } else if (cCnt > bCnt) {
                    spec[exc] = 0;
                } else {
                    spec[exc] = spec[k[0]];
                }
            }
        } else {
            uk.pb(ext);
            vi cnts;

            for (int exc : uk) {
                vi base;
                for (int inc : uk) {
                    if (inc != exc) {
                        base.pb(inc);
                    }
                }
                cnts.pb(query(base));
            }

            int cMax = *max_element(cnts.begin(), cnts.end());
            int cMin = *min_element(cnts.begin(), cnts.end());

            if (cMax == cMin) {
                for (int x : uk) {
                    spec[x] = 0;
                }
            } else {
                for (int i = 0; i < uk.size(); i++) {
                    if (cnts[i] == cMin) {
                        spec[uk[i]] = 1;
                    } else {
                        assert(cnts[i] == cMax);
                        spec[uk[i]] = 0;
                    }
                }
            }
        }
    }
}

void find(vi &edges, int cnt) {
    if (cnt == 0) {
        for (int x : edges) {
            spec[x] = 0;
        }
        return;
    }

    if (edges.size() == cnt) {
        for (int x : edges) {
            spec[x] = 1;
        }
        return;
    }

    int half = edges.size() / 2;
    vi g1, g2;

    for (int i = 0; i < half; i++) {
        g1.pb(edges[i]);
    }
    for (int i = half; i < edges.size(); i++) {
        g2.pb(edges[i]);
    }

    int c1 = queryForest(g1);
    find(g1, c1);
    find(g2, cnt - c1);
}

void calcRem() {
    for (int i = 0; i < N; i++) {
        vi search;
        for (pi x : aL[i]) {
            if (spec[x.s] == -1) {
                search.pb(x.s);
            }
        }
        if (search.empty()) continue;

        int tCnt = queryForest(search);
        find(search, tCnt);
    }
}

vi find_roads(int i0, vi i1, vi i2) {
    N = i0;
    u = i1;
    v = i2;
    M = i1.size();

    dsu_init();

    inTr.resize(M);
    fill(inTr.begin(), inTr.end(), false);
    trA.resize(N);
    aL.resize(N);

    for (int i = 0; i < M; i++) {
        if (merge(u[i], v[i])) {
            tr.pb(i);
            inTr[i] = true;
            trA[u[i]].pb({v[i], i});
            trA[v[i]].pb({u[i], i});
        }
        aL[u[i]].pb({v[i], i});
        aL[v[i]].pb({u[i], i});
    }

    spec.resize(M);
    fill(spec.begin(), spec.end(), -1);

    calcTree();

    for (int x : tr) {
        if (spec[x] == -1) { // bridges
            spec[x] = 1;
        }
    }

    calcRem();

    vi ans;
    for (int i = 0; i < M; i++) {
        if (spec[i]) {
            ans.pb(i);
        }
    }
    return ans;
}

Compilation message

In file included from /usr/include/c++/7/cassert:44:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
                 from simurgh.cpp:1:
simurgh.cpp: In function 'int query(vi&)':
simurgh.cpp:133:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     assert(used.size() == N - 1);
            ~~~~~~~~~~~~^~~~~~~~
simurgh.cpp: In function 'int queryForest(vi&)':
simurgh.cpp:153:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     assert(used.size() == N - 1);
            ~~~~~~~~~~~~^~~~~~~~
simurgh.cpp: In function 'void calcTree()':
simurgh.cpp:227:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (int i = 0; i < uk.size(); i++) {
                                 ~~^~~~~~~~~~~
simurgh.cpp: In function 'void find(vi&, int)':
simurgh.cpp:248:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (edges.size() == cnt) {
         ~~~~~~~~~~~~~^~~~~~
simurgh.cpp:261:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = half; i < edges.size(); i++) {
                        ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 256 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 252 KB correct
8 Correct 2 ms 256 KB correct
9 Correct 2 ms 256 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 256 KB correct
12 Correct 2 ms 380 KB correct
13 Correct 2 ms 376 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 256 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 252 KB correct
8 Correct 2 ms 256 KB correct
9 Correct 2 ms 256 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 256 KB correct
12 Correct 2 ms 380 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 376 KB correct
15 Correct 4 ms 376 KB correct
16 Correct 4 ms 376 KB correct
17 Correct 5 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 4 ms 376 KB correct
20 Correct 4 ms 376 KB correct
21 Correct 4 ms 376 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 3 ms 380 KB correct
24 Correct 3 ms 376 KB correct
25 Correct 2 ms 256 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 376 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 428 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 256 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 252 KB correct
8 Correct 2 ms 256 KB correct
9 Correct 2 ms 256 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 256 KB correct
12 Correct 2 ms 380 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 376 KB correct
15 Correct 4 ms 376 KB correct
16 Correct 4 ms 376 KB correct
17 Correct 5 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 4 ms 376 KB correct
20 Correct 4 ms 376 KB correct
21 Correct 4 ms 376 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 3 ms 380 KB correct
24 Correct 3 ms 376 KB correct
25 Correct 2 ms 256 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 376 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 428 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 92 ms 2172 KB correct
35 Correct 90 ms 2044 KB correct
36 Correct 71 ms 1648 KB correct
37 Correct 19 ms 504 KB correct
38 Correct 90 ms 2040 KB correct
39 Correct 78 ms 1912 KB correct
40 Correct 66 ms 1660 KB correct
41 Correct 93 ms 2132 KB correct
42 Correct 93 ms 2024 KB correct
43 Correct 35 ms 1400 KB correct
44 Correct 30 ms 1016 KB correct
45 Correct 35 ms 1144 KB correct
46 Correct 28 ms 1016 KB correct
47 Correct 16 ms 632 KB correct
48 Correct 7 ms 376 KB correct
49 Correct 10 ms 504 KB correct
50 Correct 17 ms 760 KB correct
51 Correct 33 ms 1144 KB correct
52 Correct 30 ms 1016 KB correct
53 Correct 29 ms 1016 KB correct
54 Correct 36 ms 1400 KB correct
55 Correct 35 ms 1144 KB correct
56 Correct 35 ms 1272 KB correct
57 Correct 36 ms 1252 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 334 ms 5288 KB correct
4 Correct 589 ms 7416 KB correct
5 Correct 594 ms 7544 KB correct
6 Correct 580 ms 7544 KB correct
7 Correct 513 ms 7544 KB correct
8 Correct 544 ms 7544 KB correct
9 Correct 596 ms 7544 KB correct
10 Correct 609 ms 7544 KB correct
11 Correct 608 ms 7416 KB correct
12 Correct 608 ms 7416 KB correct
13 Correct 2 ms 256 KB correct
14 Correct 579 ms 7544 KB correct
15 Correct 600 ms 7544 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB correct
2 Correct 2 ms 376 KB correct
3 Correct 2 ms 376 KB correct
4 Correct 2 ms 376 KB correct
5 Correct 2 ms 256 KB correct
6 Correct 2 ms 376 KB correct
7 Correct 2 ms 252 KB correct
8 Correct 2 ms 256 KB correct
9 Correct 2 ms 256 KB correct
10 Correct 2 ms 376 KB correct
11 Correct 2 ms 256 KB correct
12 Correct 2 ms 380 KB correct
13 Correct 2 ms 376 KB correct
14 Correct 4 ms 376 KB correct
15 Correct 4 ms 376 KB correct
16 Correct 4 ms 376 KB correct
17 Correct 5 ms 376 KB correct
18 Correct 3 ms 376 KB correct
19 Correct 4 ms 376 KB correct
20 Correct 4 ms 376 KB correct
21 Correct 4 ms 376 KB correct
22 Correct 3 ms 376 KB correct
23 Correct 3 ms 380 KB correct
24 Correct 3 ms 376 KB correct
25 Correct 2 ms 256 KB correct
26 Correct 3 ms 376 KB correct
27 Correct 3 ms 376 KB correct
28 Correct 3 ms 376 KB correct
29 Correct 2 ms 376 KB correct
30 Correct 3 ms 376 KB correct
31 Correct 3 ms 428 KB correct
32 Correct 3 ms 376 KB correct
33 Correct 3 ms 376 KB correct
34 Correct 92 ms 2172 KB correct
35 Correct 90 ms 2044 KB correct
36 Correct 71 ms 1648 KB correct
37 Correct 19 ms 504 KB correct
38 Correct 90 ms 2040 KB correct
39 Correct 78 ms 1912 KB correct
40 Correct 66 ms 1660 KB correct
41 Correct 93 ms 2132 KB correct
42 Correct 93 ms 2024 KB correct
43 Correct 35 ms 1400 KB correct
44 Correct 30 ms 1016 KB correct
45 Correct 35 ms 1144 KB correct
46 Correct 28 ms 1016 KB correct
47 Correct 16 ms 632 KB correct
48 Correct 7 ms 376 KB correct
49 Correct 10 ms 504 KB correct
50 Correct 17 ms 760 KB correct
51 Correct 33 ms 1144 KB correct
52 Correct 30 ms 1016 KB correct
53 Correct 29 ms 1016 KB correct
54 Correct 36 ms 1400 KB correct
55 Correct 35 ms 1144 KB correct
56 Correct 35 ms 1272 KB correct
57 Correct 36 ms 1252 KB correct
58 Correct 2 ms 376 KB correct
59 Correct 2 ms 376 KB correct
60 Correct 334 ms 5288 KB correct
61 Correct 589 ms 7416 KB correct
62 Correct 594 ms 7544 KB correct
63 Correct 580 ms 7544 KB correct
64 Correct 513 ms 7544 KB correct
65 Correct 544 ms 7544 KB correct
66 Correct 596 ms 7544 KB correct
67 Correct 609 ms 7544 KB correct
68 Correct 608 ms 7416 KB correct
69 Correct 608 ms 7416 KB correct
70 Correct 2 ms 256 KB correct
71 Correct 579 ms 7544 KB correct
72 Correct 600 ms 7544 KB correct
73 Correct 2 ms 376 KB correct
74 Correct 598 ms 7472 KB correct
75 Correct 576 ms 7292 KB correct
76 Correct 225 ms 3040 KB correct
77 Correct 592 ms 7416 KB correct
78 Correct 620 ms 7416 KB correct
79 Correct 616 ms 7516 KB correct
80 Correct 585 ms 7436 KB correct
81 Correct 450 ms 6648 KB correct
82 Correct 587 ms 7452 KB correct
83 Correct 376 ms 4216 KB correct
84 Correct 259 ms 4984 KB correct
85 Correct 242 ms 4728 KB correct
86 Correct 173 ms 3064 KB correct
87 Correct 127 ms 2688 KB correct
88 Correct 111 ms 1952 KB correct
89 Correct 95 ms 1912 KB correct
90 Correct 85 ms 1656 KB correct
91 Correct 31 ms 532 KB correct
92 Correct 20 ms 376 KB correct
93 Correct 228 ms 4728 KB correct
94 Correct 164 ms 3320 KB correct
95 Correct 160 ms 3192 KB correct
96 Correct 96 ms 1784 KB correct
97 Correct 114 ms 1912 KB correct
98 Correct 123 ms 2680 KB correct
99 Correct 108 ms 2040 KB correct
100 Correct 45 ms 760 KB correct
101 Correct 25 ms 504 KB correct
102 Correct 239 ms 4088 KB correct
103 Correct 234 ms 3960 KB correct
104 Correct 241 ms 3960 KB correct
105 Correct 238 ms 3960 KB correct