Submission #684059

# Submission time Handle Problem Language Result Execution time Memory
684059 2023-01-20T08:27:18 Z piOOE Simurgh (IOI17_simurgh) C++17
100 / 100
939 ms 5148 KB
#include "simurgh.h"
#include <bits/stdc++.h>

using namespace std;

struct DSU {
    vector<int> p;

    DSU() = default;

    DSU(int n) : p(n) {
        iota(p.begin(), p.end(), 0);
    }

    int leader(int x) {
        while (x != p[x]) x = p[x] = p[p[x]];
        return x;
    }

    bool unite(int x, int y) {
        x = leader(x), y = leader(y);
        if (x == y) {
            return false;
        }
        p[y] = x;
        return true;
    }

    bool same(int x, int y) {
        return leader(x) == leader(y);
    }
};

vector<int> find_roads(int n, std::vector<int> U, std::vector<int> V) {
    const int m = U.size();
    vector<int> type(m, -1);

    vector idx(n, vector<int>(n, -1));
    for (int i = 0; i < m; ++i) {
        idx[U[i]][V[i]] = idx[V[i]][U[i]] = i;
    }

    DSU d1(n);
    vector<int> st, par(n, -1), tin(n, -1);
    vector<bool> in_st(m);


    int T = 0;
    auto dfs = [&](auto self, int v) -> void {
        tin[v] = T++;
        for (int to = 0; to < n; ++to) {
            if (tin[to] == -1 && idx[v][to] != -1) {
                par[to] = v;
                in_st[idx[v][to]] = true;
                st.push_back(idx[v][to]);
                self(self, to);
            }
        }
    };

    dfs(dfs, 0);

    auto path = [&](int x, int y) {
        if (tin[x] > tin[y]) {
            swap(x, y);
        }
        vector<int> ans;
        while (y != x) {
            ans.push_back(idx[y][par[y]]);
            y = par[y];
        }
        return ans;
    };

    const int init = count_common_roads(st);

    for (int i = 0; i < m; ++i) {
        if (!in_st[i]) {
            auto p = path(U[i], V[i]);
            sort(p.begin(), p.end());

            int done = -1, not_done = -1;
            for (int x: p) {
                if (type[x] != -1) {
                    done = x;
                } else {
                    not_done = x;
                }
            }

            if (done != -1) {
                if (not_done == -1) {
                    continue;
                }
                set<int> sp(st.begin(), st.end());
                sp.insert(i);

                sp.erase(done);
                int aim = count_common_roads(vector(sp.begin(), sp.end()));
                sp.insert(done);
                for (int x: p) {
                    if (type[x] == -1) {
                        sp.erase(x);
                        int now = count_common_roads(vector(sp.begin(), sp.end()));
                        sp.insert(x);
                        type[x] = type[done] ^ (now != aim);
                    }
                }
            } else {
                set<int> sp(st.begin(), st.end());
                sp.insert(i);

                int mx = init, mn = init;
                vector<pair<int, int>> save{{init, i}};
                for (int x: p) {
                    sp.erase(x);
                    int now = count_common_roads(vector(sp.begin(), sp.end()));
                    sp.insert(x);
                    mx = max(mx, now), mn = min(mn, now);
                    save.emplace_back(now, x);
                }

                for (auto [now, x]: save) {
                    if (mn == mx) {
                        type[x] = 0;
                    } else {
                        type[x] = now != mx;
                    }
                }
            }
        }
    }
    for (int i : st) {
        if (type[i] == -1) {
            type[i] = 1;
        }
    }

    auto howMany = [&](vector<int> a) {
        DSU d(n);
        for (int i: a) {
            assert(d.unite(U[i], V[i]));
        }
        int cnt = 0;
        for (int p : st) {
            if (d.unite(U[p], V[p])) {
                a.push_back(p);
                cnt += type[p];
            }
        }

        assert(a.size() == n - 1);
        return count_common_roads(a) - cnt;
    };

    for (int v = 0; v < n; ++v) {
        vector<int> need;
        for (int to = 0; to < n; ++to) {
            if (idx[v][to] != -1 && type[idx[v][to]] == -1) {
                need.push_back(idx[v][to]);
            }
        }

        auto solve = [&](auto self, vector<int> a, int cnt) -> void {
            if (cnt == 0) {
                for (int i : a) {
                    type[i] = 0;
                }
                return;
            }
            if (a.size() == 1) {
                type[a[0]] = 1;
                return;
            }
            vector<int> L(a.begin(), a.begin() + a.size() / 2);
            vector<int> R(a.begin() + a.size() / 2, a.end());
            int have = howMany(L);
            self(self, L, have);
            self(self, R, cnt - have);
        };
        solve(solve, need, howMany(need));
    }

    st.clear();
    for (int i = 0; i < m; ++i) {
        assert(type[i] != -1);
        if (type[i] == 1) {
            st.push_back(i);
        }
    }

    assert(st.size() == n - 1);

    return st;
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from simurgh.cpp:2:
simurgh.cpp: In lambda function:
simurgh.cpp:152:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  152 |         assert(a.size() == n - 1);
      |                ~~~~~~~~~^~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:192:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  192 |     assert(st.size() == n - 1);
      |            ~~~~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB correct
2 Correct 1 ms 300 KB correct
3 Correct 0 ms 300 KB correct
4 Correct 1 ms 296 KB correct
5 Correct 0 ms 300 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 1 ms 296 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 1 ms 296 KB correct
12 Correct 1 ms 212 KB correct
13 Correct 1 ms 212 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB correct
2 Correct 1 ms 300 KB correct
3 Correct 0 ms 300 KB correct
4 Correct 1 ms 296 KB correct
5 Correct 0 ms 300 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 1 ms 296 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 1 ms 296 KB correct
12 Correct 1 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 2 ms 340 KB correct
15 Correct 2 ms 340 KB correct
16 Correct 2 ms 340 KB correct
17 Correct 2 ms 340 KB correct
18 Correct 1 ms 212 KB correct
19 Correct 2 ms 340 KB correct
20 Correct 2 ms 340 KB correct
21 Correct 2 ms 340 KB correct
22 Correct 1 ms 340 KB correct
23 Correct 1 ms 340 KB correct
24 Correct 1 ms 300 KB correct
25 Correct 1 ms 308 KB correct
26 Correct 1 ms 340 KB correct
27 Correct 1 ms 212 KB correct
28 Correct 1 ms 300 KB correct
29 Correct 1 ms 308 KB correct
30 Correct 2 ms 312 KB correct
31 Correct 1 ms 212 KB correct
32 Correct 2 ms 212 KB correct
33 Correct 1 ms 304 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB correct
2 Correct 1 ms 300 KB correct
3 Correct 0 ms 300 KB correct
4 Correct 1 ms 296 KB correct
5 Correct 0 ms 300 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 1 ms 296 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 1 ms 296 KB correct
12 Correct 1 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 2 ms 340 KB correct
15 Correct 2 ms 340 KB correct
16 Correct 2 ms 340 KB correct
17 Correct 2 ms 340 KB correct
18 Correct 1 ms 212 KB correct
19 Correct 2 ms 340 KB correct
20 Correct 2 ms 340 KB correct
21 Correct 2 ms 340 KB correct
22 Correct 1 ms 340 KB correct
23 Correct 1 ms 340 KB correct
24 Correct 1 ms 300 KB correct
25 Correct 1 ms 308 KB correct
26 Correct 1 ms 340 KB correct
27 Correct 1 ms 212 KB correct
28 Correct 1 ms 300 KB correct
29 Correct 1 ms 308 KB correct
30 Correct 2 ms 312 KB correct
31 Correct 1 ms 212 KB correct
32 Correct 2 ms 212 KB correct
33 Correct 1 ms 304 KB correct
34 Correct 103 ms 1276 KB correct
35 Correct 98 ms 1356 KB correct
36 Correct 73 ms 1072 KB correct
37 Correct 11 ms 608 KB correct
38 Correct 102 ms 1320 KB correct
39 Correct 90 ms 1216 KB correct
40 Correct 71 ms 1060 KB correct
41 Correct 108 ms 1312 KB correct
42 Correct 103 ms 1352 KB correct
43 Correct 43 ms 952 KB correct
44 Correct 33 ms 880 KB correct
45 Correct 37 ms 932 KB correct
46 Correct 30 ms 844 KB correct
47 Correct 15 ms 704 KB correct
48 Correct 7 ms 468 KB correct
49 Correct 10 ms 604 KB correct
50 Correct 16 ms 712 KB correct
51 Correct 38 ms 852 KB correct
52 Correct 41 ms 1008 KB correct
53 Correct 29 ms 852 KB correct
54 Correct 42 ms 964 KB correct
55 Correct 59 ms 956 KB correct
56 Correct 58 ms 952 KB correct
57 Correct 63 ms 964 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 459 ms 3216 KB correct
4 Correct 908 ms 4760 KB correct
5 Correct 910 ms 4760 KB correct
6 Correct 939 ms 4840 KB correct
7 Correct 870 ms 4832 KB correct
8 Correct 881 ms 4836 KB correct
9 Correct 894 ms 4816 KB correct
10 Correct 903 ms 4764 KB correct
11 Correct 927 ms 5148 KB correct
12 Correct 920 ms 4764 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 893 ms 4884 KB correct
15 Correct 914 ms 4832 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB correct
2 Correct 1 ms 300 KB correct
3 Correct 0 ms 300 KB correct
4 Correct 1 ms 296 KB correct
5 Correct 0 ms 300 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 1 ms 296 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 1 ms 296 KB correct
12 Correct 1 ms 212 KB correct
13 Correct 1 ms 212 KB correct
14 Correct 2 ms 340 KB correct
15 Correct 2 ms 340 KB correct
16 Correct 2 ms 340 KB correct
17 Correct 2 ms 340 KB correct
18 Correct 1 ms 212 KB correct
19 Correct 2 ms 340 KB correct
20 Correct 2 ms 340 KB correct
21 Correct 2 ms 340 KB correct
22 Correct 1 ms 340 KB correct
23 Correct 1 ms 340 KB correct
24 Correct 1 ms 300 KB correct
25 Correct 1 ms 308 KB correct
26 Correct 1 ms 340 KB correct
27 Correct 1 ms 212 KB correct
28 Correct 1 ms 300 KB correct
29 Correct 1 ms 308 KB correct
30 Correct 2 ms 312 KB correct
31 Correct 1 ms 212 KB correct
32 Correct 2 ms 212 KB correct
33 Correct 1 ms 304 KB correct
34 Correct 103 ms 1276 KB correct
35 Correct 98 ms 1356 KB correct
36 Correct 73 ms 1072 KB correct
37 Correct 11 ms 608 KB correct
38 Correct 102 ms 1320 KB correct
39 Correct 90 ms 1216 KB correct
40 Correct 71 ms 1060 KB correct
41 Correct 108 ms 1312 KB correct
42 Correct 103 ms 1352 KB correct
43 Correct 43 ms 952 KB correct
44 Correct 33 ms 880 KB correct
45 Correct 37 ms 932 KB correct
46 Correct 30 ms 844 KB correct
47 Correct 15 ms 704 KB correct
48 Correct 7 ms 468 KB correct
49 Correct 10 ms 604 KB correct
50 Correct 16 ms 712 KB correct
51 Correct 38 ms 852 KB correct
52 Correct 41 ms 1008 KB correct
53 Correct 29 ms 852 KB correct
54 Correct 42 ms 964 KB correct
55 Correct 59 ms 956 KB correct
56 Correct 58 ms 952 KB correct
57 Correct 63 ms 964 KB correct
58 Correct 1 ms 212 KB correct
59 Correct 0 ms 212 KB correct
60 Correct 459 ms 3216 KB correct
61 Correct 908 ms 4760 KB correct
62 Correct 910 ms 4760 KB correct
63 Correct 939 ms 4840 KB correct
64 Correct 870 ms 4832 KB correct
65 Correct 881 ms 4836 KB correct
66 Correct 894 ms 4816 KB correct
67 Correct 903 ms 4764 KB correct
68 Correct 927 ms 5148 KB correct
69 Correct 920 ms 4764 KB correct
70 Correct 1 ms 212 KB correct
71 Correct 893 ms 4884 KB correct
72 Correct 914 ms 4832 KB correct
73 Correct 0 ms 212 KB correct
74 Correct 909 ms 4836 KB correct
75 Correct 888 ms 4716 KB correct
76 Correct 289 ms 2252 KB correct
77 Correct 922 ms 4756 KB correct
78 Correct 922 ms 4764 KB correct
79 Correct 911 ms 4760 KB correct
80 Correct 890 ms 4656 KB correct
81 Correct 764 ms 4156 KB correct
82 Correct 894 ms 4652 KB correct
83 Correct 489 ms 3060 KB correct
84 Correct 421 ms 3404 KB correct
85 Correct 346 ms 3140 KB correct
86 Correct 199 ms 2572 KB correct
87 Correct 122 ms 2132 KB correct
88 Correct 101 ms 2032 KB correct
89 Correct 94 ms 1948 KB correct
90 Correct 83 ms 1868 KB correct
91 Correct 35 ms 1408 KB correct
92 Correct 25 ms 1364 KB correct
93 Correct 376 ms 3128 KB correct
94 Correct 185 ms 2732 KB correct
95 Correct 167 ms 2508 KB correct
96 Correct 87 ms 1876 KB correct
97 Correct 115 ms 2132 KB correct
98 Correct 135 ms 2324 KB correct
99 Correct 99 ms 2052 KB correct
100 Correct 49 ms 1500 KB correct
101 Correct 27 ms 1376 KB correct
102 Correct 494 ms 3080 KB correct
103 Correct 481 ms 3108 KB correct
104 Correct 486 ms 3112 KB correct
105 Correct 489 ms 3116 KB correct