Submission #966720

# Submission time Handle Problem Language Result Execution time Memory
966720 2024-04-20T08:59:42 Z fskarica Parking (CEOI22_parking) C++17
0 / 100
108 ms 22372 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define fi first
#define se second
#define pii pair<int, int>

const int MAX = 2e5 + 10;
int n, m;
int x, y;
int arr[MAX][2];
vector <int> boja[MAX];
vector <int> prazno;
vector <pii> sol;
int veze[MAX];
int bio[MAX];
vector <pii> v;

void rek(int pos) {
    if (!veze[pos]) return;
    if (bio[veze[pos]]) return;

    bio[veze[pos]] = 1;
    rek(veze[pos]);
}

void rek2(int pos) {
    if (!veze[pos]) return;

    v.push_back({pos, veze[pos]});
    bio[veze[pos]] = 1;
    rek2(veze[pos]);
}

int main() {
    cin >> n >> m;

    for (int i = 1; i <= m; i++) {
        cin >> x >> y;

        arr[i][0] = x;
        arr[i][1] = y;

        if (x == 0) prazno.push_back(i);

        boja[x].push_back(i);
        boja[y].push_back(i);
    }

    for (int i = 1; i <= m; i++) boja[i].clear();
    for (int i = 1; i <= m; i++) {
        x = arr[i][0];
        y = arr[i][1];

        boja[x].push_back(i);
        boja[y].push_back(i);
    }

    for (int i = 1; i <= n; i++) {
        x = boja[i][0];
        y = boja[i][1];

        if (x == y) continue;

        if (arr[x][0] == i && arr[y][0] == i && arr[x][1] == 0 && arr[y][1] == 0) {
            arr[y][1] = i;
            arr[x][0] = 0;

            prazno.push_back(x);

            sol.push_back({x, y});
        }
    }

    for (int i = 1; i <= m; i++) boja[i].clear();
    for (int i = 1; i <= m; i++) {
        x = arr[i][0];
        y = arr[i][1];

        boja[x].push_back(i);
        boja[y].push_back(i);
    }

    for (int i = 1; i <= n; i++) {
        x = boja[i][0];
        y = boja[i][1];

        if (x == y) continue;

        if (arr[x][1] == i && arr[y][1] == i) {
            if (prazno.empty()) {cout << "-1\n"; return 0;}

            arr[prazno.back()][0] = i;
            arr[prazno.back()][1] = i;

            arr[x][1] = 0;
            arr[y][1] = 0;

            sol.push_back({x, prazno.back()});
            sol.push_back({y, prazno.back()});

            prazno.pop_back();
        }
    }

//    cout << "gotova prva faza\n";
//    for (int i = 1; i <= m; i++) {
//        cout << arr[i][0] << " " << arr[i][1] << "\n";
//    }
//    cout << "\n\n";

    for (int i = 1; i <= m; i++) boja[i].clear();
    for (int i = 1; i <= m; i++) {
        x = arr[i][0];
        y = arr[i][1];

        boja[x].push_back(i);
        boja[y].push_back(i);
    }

    for (int i = 1; i <= n; i++)  {
        x = boja[i][0];
        y = boja[i][1];

        if (x == y) continue;

        if (arr[x][1] == i && arr[y][0] == i) veze[x] = y;
        if (arr[x][0] == i && arr[y][1] == i) veze[y] = x;
    }

    for (int i = 1; i <= m; i++) {
        if (bio[i]) continue;
        if (!veze[i]) continue;

        rek(i);

        if (bio[i]) {
            if (prazno.empty()) {cout << "-1\n"; return 0;}

            arr[prazno.back()][0] = arr[i][1];
            arr[i][1] = 0;

            sol.push_back({i, prazno.back()});
            prazno.pop_back();
        }
    }

//    cout << "gotova druga faza\n";
//    for (int i = 1; i <= m; i++) {
//        cout << arr[i][0] << " " << arr[i][1] << "\n";
//    }
//    cout << "\n\n";

    for (int i = 1; i <= m; i++) boja[i].clear();
    for (int i = 1; i <= m; i++) {
        x = arr[i][0];
        y = arr[i][1];

        boja[x].push_back(i);
        boja[y].push_back(i);
    }

    memset(veze, 0, sizeof(veze));
    for (int i = 1; i <= n; i++)  {
        x = boja[i][0];
        y = boja[i][1];

        if (x == y) continue;

        if (arr[x][1] == i && arr[y][0] == i) veze[x] = y;
        if (arr[x][0] == i && arr[y][1] == i) veze[y] = x;
    }

//    cout << "veze - "; for (int i = 1; i <= m; i++) cout << veze[i] << " "; cout << "\n";

    memset(bio, 0, sizeof(bio));
    for (int i = 1; i <= m; i++) {
        if (bio[i]) continue;
        if (!veze[i]) continue;

        v.clear();
        rek2(i);

        reverse(v.begin(), v.end());
        for (auto e : v) {
            x = e.fi;
            y = e.se;

            arr[y][1] = arr[x][1];
            arr[x][1] = 0;

            sol.push_back({x, y});
        }
    }

//    cout << "gotova treca faza\n";
//    for (int i = 1; i <= m; i++) {
//        cout << arr[i][0] << " " << arr[i][1] << "\n";
//    }
//    cout << "\n\n";

    for (int i = 1; i <= m; i++) boja[i].clear();
    for (int i = 1; i <= m; i++) {
        x = arr[i][0];
        y = arr[i][1];

        boja[x].push_back(i);
        boja[y].push_back(i);
    }

    for (int i = 1; i <= n; i++) {
        x = boja[i][0];
        y = boja[i][1];

        if (x == y) continue;

        if (arr[x][0] == i && arr[y][0] == i) {
            arr[y][1] = i;
            arr[x][0] = 0;

            sol.push_back({x, y});
        }
    }

//    cout << "gotova cetvrta faza\n";
//    for (int i = 1; i <= m; i++) {
//        cout << arr[i][0] << " " << arr[i][1] << "\n";
//    }
//    cout << "\n\n";

    cout << sol.size() << "\n";

    for (auto e : sol) {
        cout << e.fi << " " << e.se << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 108 ms 22372 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 6748 KB Output isn't correct
2 Halted 0 ms 0 KB -