Submission #853993

# Submission time Handle Problem Language Result Execution time Memory
853993 2023-09-25T18:39:57 Z FairyWinx Longest Trip (IOI23_longesttrip) C++17
100 / 100
10 ms 968 KB
#include <bits/stdc++.h>
#ifndef LOCAL
    #include "longesttrip.h"
#endif

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

using namespace std;

#ifdef LOCAL
vector<vector<int>> G_secret;
int n;
int count_query = 0;

bool are_connected(vector<int> a, vector<int> b) {
    ++count_query;
    for (int i : a) {
        for (int j : b) {
            if (G_secret[i][j])
                return true;
        }
    }
    return false;
}
#endif

struct dsu {
    vector<int> p;

    dsu(int n) {
        p.resize(n);
        iota(all(p), 0);
    }

    int get(int a) {
        if (p[a] == a)
            return a;
        return p[a] = get(p[a]);
    }

    void merge(int a, int b) {
        a = get(a), b = get(b);
        p[a] = b;
    }
};

vector<int> get_comp(int n, int v, dsu &d) {
    vector<int> comp;
    for (int i = 0; i < n; ++i) {
        if (d.get(i) == v) {
            comp.push_back(i);
        }
    }
    return comp;
}

void add_comp(vector<int> &ans, vector<int> &comp, pair<int, int> end) {
    ans.push_back(end.first);
    for (int j : comp) {
        if (j != end.first && j != end.second) {
            ans.push_back(j);
        }
    }
    if (end.first != end.second) {
        ans.push_back(end.second);
    }
}

vector<int> longest_trip(int n, int D) { // Случай, когда отдельная вершинка - 0
    dsu d(n);
    vector<vector<int>> path;
    vector<pair<int, int>> ends;
    ends.push_back({0, 0});
    path.push_back({0});
    vector<int> ost(n - 1);
    iota(all(ost), 1);
    while (true) {
        int ind_comp = ost[0];
        bool find = false;
        bool was_merge = false;
        for (int i = 0; i < (int) ost.size(); ++i) {
            if (are_connected({ends.back().second}, get_comp(n, ost[i], d))) {
                path.push_back(get_comp(n, ost[i], d));
                int last_end = ends.back().second;
                ends.emplace_back();

                {
                    int L = -1, R = (int) path.back().size() - 1;
                    while (R - L > 1) {
                        // cout << L << ' ' << R << "!\n";
                        int mid = (R + L) / 2;
                        vector<int> tmp;
                        for (int j = 0; j <= mid; ++j) {
                            tmp.push_back(path.back()[j]);
                        }
                        if (tmp.size() && are_connected({last_end}, tmp)) {
                            R = mid;
                        } else {
                            L = mid;
                        }
                    }
                    ends.back().first = path.back()[R];
                }
                if (path.back().size() == 1) {
                    ends.back().second = ends.back().first;
                } else {
                    if (ends.back().first != path.back()[0]) {
                        ends.back().second = path.back()[0];
                    } else {
                        ends.back().second = path.back().back();
                    }
                }
                swap(ost[i], ost.back());
                ost.pop_back();
                find = true;
                break;
            } else {
                was_merge = true;
                d.merge(ost[i], ind_comp);
                swap(ost[i], ost.back());
                ost.pop_back();
                --i;
            }
        }
        if (was_merge)
            ost.push_back(ind_comp);
        if (!ost.size()) {
            vector<int> ans;
            for (int i = 0; i < (int) path.size(); ++i) {
                add_comp(ans, path[i], ends[i]);
            }
            // cout << "MEOW1\n";
            return ans;
        }
        if (!find) {
            vector<int> last_comp = get_comp(n, ost[0], d);
            int V = -1;
            vector<int> ost_graph;
            for (auto i : path) {
                for (int j : i) {
                    ost_graph.push_back(j);
                }
            }
            {
                int L = -1, R = (int) last_comp.size() - 1;
                while (R - L > 1) {
                    // cout << L << ' ' << R << "!\n";
                    int mid = (R + L) / 2;
                    vector<int> tmp;
                    for (int j = 0; j <= mid; ++j) {
                        tmp.push_back(last_comp[j]);
                    }
                    if (tmp.size() && are_connected(ost_graph, tmp)) {
                        R = mid;
                    } else {
                        L = mid;
                    }
                }
                V = last_comp[R];
            }
            if (!are_connected({V}, ost_graph)) {
                V = -1;
            }
                // cout << V << ':';
            // for (int i : last_comp) {
            //     if (are_connected({i}, ost_graph)) {
            //         V = i;
            //         cout << V << '\n';
            //         break;
            //     }
            // }
            // cout << V << '\n';
            if (V == -1) {
                vector<int> ans;
                if (last_comp.size() > n - last_comp.size()) {
                    for (int i : last_comp)
                        ans.push_back(i);
                } else {
                    for (auto i : path) {
                        for (int j : i)
                            ans.push_back(j);
                    }
                }
                return ans;
            }
            // {
                int L = -1, R = (int) path.size() - 1;
                while (R - L > 1) {
                    // cout << L << ' ' << R << "!\n";
                    int mid = (R + L) / 2;
                    vector<int> tmp;
                    for (int j = 0; j <= mid; ++j) {
                        tmp.push_back(ends[j].first);
                    }
                    if (tmp.size() && are_connected({V}, tmp)) {
                        R = mid;
                    } else {
                        L = mid;
                    }
                }
            // }
            for (int i = R; i < (int) path.size(); ++i) {
                if (are_connected({V}, {ends[i].first})) {
                    vector<int> ans;
                    for (int j : last_comp) {
                        if (j != V) {
                            ans.push_back(j);
                        }
                    }
                    ans.push_back(V);
                    for (int j = i; j < (int) path.size(); ++j) {
                        add_comp(ans, path[j], ends[j]);
                    }
                    for (int j = 0; j < i; ++j) {
                        add_comp(ans, path[j], ends[j]);
                    }
                    // cout << "MEOW2\n";
                    return ans;
                }
            }
        }
    }
    assert(0);
    return {-1};
}

#ifdef LOCAL

int main()
{
    int t;
    cin >> t;
    while (t--) {
        count_query = 0;
        int n, d;
        cin >> n >> d;
        G_secret.clear();
        G_secret.resize(n, vector<int> (n));
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                cin >> G_secret[i][j];
                G_secret[j][i] = G_secret[i][j];
            }
        }
        cout << "MEOW\n";
        auto res = longest_trip(n, d);
        // cout << res.size() << '\n';
        // for (int i : res)
        //     cout << i << ' ';
        cout << '\n';
        cout << count_query << '\n';
        // vector<int> p(n);
        // iota(all(p), 0);
        // int ans = 0;
        // do {
        //     for (int i = 1; i <= n; ++i) {
        //         bool valid = true;
        //         for (int j = 0; j < i - 1; ++j) {
        //             if (!G_secret[p[j]][p[j + 1]]) {
        //                 valid = false;
        //             }
        //         }
        //         if (valid) {
        //             ans = max(ans, i);
        //         }
        //     }
        // } while (next_permutation(all(p)));
        // if (res.size() != ans) {
        //     cout << "Попався\n";
        //     cout << n << ' ' << d << '\n';
        //     for (int i = 1; i < n; ++i) {
        //         for (int j = 0; j < i; ++j) {
        //             cout << G_secret[i][j] << ' ';
        //         }
        //         cout << '\n';
        //     }
        //     for (int i : res)
        //         cout << i << ' ';
        //     cout << '\n';
        //     cout << "! " << ans << '\n';
        //     return 0;
        // }
    }

    return 0;
}

#endif
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 340 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 600 KB Output is correct
5 Correct 7 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 4 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 600 KB Output is correct
5 Correct 6 ms 344 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 5 ms 600 KB Output is correct
9 Correct 5 ms 600 KB Output is correct
10 Correct 7 ms 600 KB Output is correct
11 Correct 7 ms 344 KB Output is correct
12 Correct 6 ms 344 KB Output is correct
13 Correct 6 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 6 ms 416 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 856 KB Output is correct
5 Correct 6 ms 344 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 6 ms 856 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 6 ms 344 KB Output is correct
11 Correct 6 ms 344 KB Output is correct
12 Correct 6 ms 600 KB Output is correct
13 Correct 6 ms 344 KB Output is correct
14 Correct 8 ms 344 KB Output is correct
15 Correct 8 ms 344 KB Output is correct
16 Correct 8 ms 344 KB Output is correct
17 Correct 6 ms 344 KB Output is correct
18 Correct 7 ms 344 KB Output is correct
19 Correct 7 ms 344 KB Output is correct
20 Correct 6 ms 344 KB Output is correct
21 Correct 6 ms 344 KB Output is correct
22 Correct 6 ms 344 KB Output is correct
23 Correct 7 ms 600 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 7 ms 344 KB Output is correct
26 Correct 8 ms 344 KB Output is correct
27 Correct 6 ms 344 KB Output is correct
28 Correct 6 ms 344 KB Output is correct
29 Correct 7 ms 344 KB Output is correct
30 Correct 6 ms 344 KB Output is correct
31 Correct 7 ms 440 KB Output is correct
32 Correct 7 ms 432 KB Output is correct
33 Correct 5 ms 344 KB Output is correct
34 Correct 7 ms 448 KB Output is correct
35 Correct 7 ms 852 KB Output is correct
36 Correct 7 ms 856 KB Output is correct
37 Correct 9 ms 344 KB Output is correct
38 Correct 9 ms 600 KB Output is correct
39 Correct 10 ms 704 KB Output is correct
40 Correct 9 ms 868 KB Output is correct
41 Correct 9 ms 700 KB Output is correct
42 Correct 9 ms 860 KB Output is correct
43 Correct 10 ms 956 KB Output is correct
44 Correct 10 ms 700 KB Output is correct
45 Correct 8 ms 344 KB Output is correct
46 Correct 8 ms 344 KB Output is correct
47 Correct 8 ms 344 KB Output is correct
48 Correct 8 ms 344 KB Output is correct
49 Correct 10 ms 344 KB Output is correct
50 Correct 6 ms 344 KB Output is correct
51 Correct 7 ms 692 KB Output is correct
52 Correct 7 ms 436 KB Output is correct
53 Correct 6 ms 600 KB Output is correct
54 Correct 7 ms 344 KB Output is correct
55 Correct 7 ms 600 KB Output is correct
56 Correct 7 ms 612 KB Output is correct
57 Correct 9 ms 872 KB Output is correct
58 Correct 9 ms 708 KB Output is correct
59 Correct 10 ms 460 KB Output is correct
60 Correct 9 ms 696 KB Output is correct
61 Correct 9 ms 924 KB Output is correct
62 Correct 9 ms 696 KB Output is correct
63 Correct 9 ms 704 KB Output is correct
64 Correct 9 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 5 ms 600 KB Output is correct
5 Correct 6 ms 600 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 5 ms 600 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 6 ms 344 KB Output is correct
11 Correct 6 ms 600 KB Output is correct
12 Correct 6 ms 344 KB Output is correct
13 Correct 6 ms 344 KB Output is correct
14 Correct 10 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 6 ms 344 KB Output is correct
17 Correct 8 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 6 ms 344 KB Output is correct
20 Correct 6 ms 340 KB Output is correct
21 Correct 7 ms 596 KB Output is correct
22 Correct 9 ms 376 KB Output is correct
23 Correct 6 ms 596 KB Output is correct
24 Correct 6 ms 344 KB Output is correct
25 Correct 7 ms 344 KB Output is correct
26 Correct 6 ms 344 KB Output is correct
27 Correct 7 ms 604 KB Output is correct
28 Correct 7 ms 440 KB Output is correct
29 Correct 6 ms 344 KB Output is correct
30 Correct 7 ms 344 KB Output is correct
31 Correct 7 ms 452 KB Output is correct
32 Correct 7 ms 344 KB Output is correct
33 Correct 8 ms 344 KB Output is correct
34 Correct 7 ms 344 KB Output is correct
35 Correct 7 ms 344 KB Output is correct
36 Correct 9 ms 344 KB Output is correct
37 Correct 6 ms 344 KB Output is correct
38 Correct 7 ms 692 KB Output is correct
39 Correct 8 ms 688 KB Output is correct
40 Correct 7 ms 704 KB Output is correct
41 Correct 7 ms 344 KB Output is correct
42 Correct 7 ms 344 KB Output is correct
43 Correct 7 ms 344 KB Output is correct
44 Correct 7 ms 344 KB Output is correct
45 Correct 6 ms 600 KB Output is correct
46 Correct 7 ms 600 KB Output is correct
47 Correct 6 ms 344 KB Output is correct
48 Correct 9 ms 852 KB Output is correct
49 Correct 9 ms 856 KB Output is correct
50 Correct 9 ms 708 KB Output is correct
51 Correct 9 ms 964 KB Output is correct
52 Correct 9 ms 456 KB Output is correct
53 Correct 9 ms 696 KB Output is correct
54 Correct 9 ms 716 KB Output is correct
55 Correct 9 ms 704 KB Output is correct
56 Correct 10 ms 344 KB Output is correct
57 Correct 9 ms 952 KB Output is correct
58 Correct 9 ms 700 KB Output is correct
59 Correct 10 ms 700 KB Output is correct
60 Correct 10 ms 704 KB Output is correct
61 Correct 9 ms 696 KB Output is correct
62 Correct 8 ms 708 KB Output is correct
63 Correct 9 ms 604 KB Output is correct
64 Correct 9 ms 460 KB Output is correct
65 Correct 9 ms 612 KB Output is correct
66 Correct 9 ms 456 KB Output is correct
67 Correct 9 ms 700 KB Output is correct
68 Correct 10 ms 704 KB Output is correct
69 Correct 9 ms 704 KB Output is correct
70 Correct 9 ms 960 KB Output is correct
71 Correct 9 ms 464 KB Output is correct
72 Correct 9 ms 716 KB Output is correct
73 Correct 9 ms 460 KB Output is correct
74 Correct 9 ms 708 KB Output is correct
75 Correct 9 ms 448 KB Output is correct
76 Correct 10 ms 600 KB Output is correct
77 Correct 9 ms 604 KB Output is correct
78 Correct 9 ms 708 KB Output is correct
79 Correct 10 ms 968 KB Output is correct
80 Correct 9 ms 864 KB Output is correct
81 Correct 9 ms 460 KB Output is correct
82 Correct 9 ms 456 KB Output is correct