Submission #683434

# Submission time Handle Problem Language Result Execution time Memory
683434 2023-01-18T11:45:41 Z nutella Parking (CEOI22_parking) C++17
0 / 100
51 ms 6028 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    vector<array<int, 2>> a(m), inv(n, {-1, -1}), ans;

    auto drivable = [&](int i, int p) {
        assert(i < n && p < 2);
        int pos = inv[i][p];

        return a[pos][0] == i || a[pos][0] == -1;
    };

    for (int i = 0; i < m; ++i) {
        cin >> a[i][1] >> a[i][0];
        --a[i][1], --a[i][0];

        for (int k : a[i]) {
            if (inv[k][0] == -1) {
                inv[k][0] = i;
            } else {
                inv[k][1] = i;
            }
        }
    }

    vector<bool> done(n);

    vector<int> free;
    for (int i = 0; i < m; ++i) {
        if (a[i][0] == -1 && a[i][1] == -1) {
            free.push_back(i);
        }
    }

    for (int i = 0; i < n; ++i) {
        if (inv[i][0] == inv[i][1]) {
            done[i] = true;
        }
    }

    while (find(done.begin(), done.end(), false) != done.end()) {
        bool any = false;
        for (int i = 0; i < n; ++i) {
            if (!done[i]) {
                int from = inv[i][0], to = inv[i][1];
                if (drivable(i, 0) && drivable(i, 1) && (a[from][0] == -1 || a[to][0] == -1)) {
                    if (a[to][0] != -1) {
                        swap(from, to);
                    }

                    done[i] = true;
                    any = true;

                    ans.push_back({from, to});

                    a[from][(a[from][1] == i)] = -1;
                    a[to][0] = i;
                    inv[i][0] = inv[i][1] = to;

                    if (a[from][1] == -1) {
                        free.push_back(from);
                    }
                }
            }
        }

        if (!any) {
            for (int i = 0; i < n; ++i) {
                if (!done[i]) {
                    int x = inv[i][0], y = inv[i][1];
                    if (drivable(i, 0) && drivable(i, 1) && !free.empty()) {
                        ans.push_back({x, free.back()});
                        ans.push_back({y, free.back()});

                        inv[i][0] = inv[i][1] = free.back();
                        free.pop_back();

                        a[x][0] = -1, a[y][0] = -1;
                        done[i] = true;
                        any = true;
                    }
                }
            }

            if (!any) {
                cout << "-1\n";
                return 0;
            }
        }
    }

    cout << ans.size() << '\n';
    for (auto [x, y] : ans) {
        cout << x + 1 << " " << y + 1 << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 444 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 51 ms 6028 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 448 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 448 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 596 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 444 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -