Cod sursa(job #1802024)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 9 noiembrie 2016 19:58:51
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

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

int n, m, l, x, y, i;
int incolo[10005], incoace[10005];
vector <int> ls[10005];
bool ok, fol[10005];

int pairup(int n) {
    if (fol[n])
        return 0;
    fol[n] = 1;
    int l = ls[n].size();
    int i, y;
    for (i = 0; i < l; i++) {
        y = ls[n][i];
        if (incoace[y] == 0) {
            incolo[n] = y;
            incoace[y] = n;
            return 1;
        }
    }
    for (i = 0; i < l; i++) {
        y = ls[n][i];
        if (pairup(incoace[y])) {
            incolo[n] = y;
            incoace[y] = n;
            return 1;
        }
    }
    return 0;
}

int main() {
    f >> n >> m >> l;
    while (l--) {
        f >> x >> y;
        ls[x].push_back(y);
    }
    for (ok = 1; ok;) {
        ok = 0;
        for (i = 1; i <= n; i++)
            fol[i] = 0;
        for (i = 1; i <= n; i++)
            if (incolo[i] == 0 && pairup(i))
                ok = 1;
    }
    for (i = 1, l = 0; i <= n; i++)
        l += (incolo[i] > 0);
    g << l << '\n';
    for (i = 1; i <= n; i++)
        if (incolo[i] > 0)
            g << i << ' ' << incolo[i]<< "\n";
    return 0;
}