Cod sursa(job #2700809)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 28 ianuarie 2021 21:36:15
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.36 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("cuplaj.in");
ofstream g ("cuplaj.out");

constexpr int NMAX = 1e4 + 5;

int N, M, E;
int L[NMAX], R[NMAX];

bool marked[NMAX];

vector <int> G[NMAX];

bool cupleaza (int node) {
    marked[node] = 1;

    for (auto it : G[node]) {
        if (R[it] == 0) {
            L[node]= it;
            R[it] = node;

            return 1;
        }
    }

    for (auto it : G[node]) {
        if (!marked[R[it]] && cupleaza(R[it])) {
            L[node] = it;
            R[it] = node;

            return 1;
        }
    }

    return 0;
}

int Read () {
    f >> N >> M >> E;

    for (int i = 1; i <= E; ++ i ) {
        int x, y;
        f >> x >> y;

        G[x].push_back(y);
    }
}

void Cuplaj () {
    bool done = 1;

    while (done) {
        done = 0;

        for (int i = 1; i <= N; ++ i )
            marked[i] = 0;

        for (int i = 1; i <= N; ++ i ) {
            if (!L[i] && !marked[i])
                done |= cupleaza(i);
        }
    }

    int cnt = 0;

    for (int i = 1; i <= N; ++ i ) {
        if (L[i]) ++ cnt;
    }
    g << cnt << '\n';

    for (int i = 1; i <= N; ++ i ) {
        if (L[i]) {
            g << i << " " << L[i] << '\n';
        }
    }
}

int main () {
    Read();

    Cuplaj();

    return 0;
}