Cod sursa(job #2554397)

Utilizator ezioconnorVlad - Gabriel Iftimescu ezioconnor Data 22 februarie 2020 21:06:51
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 1e4 + 5;

int n, m, e, cnt, pr;
int st[NMAX], dr[NMAX];
bool f[NMAX];
vector <int> g[NMAX];

inline bool cupleaza(int nod) {
    if (f[nod])
        return 0;
    f[nod] = 1;
    for (auto vecin : g[nod]) {
        if (!st[vecin]) {
            dr[nod] = vecin;
            st[vecin] = nod;
            return 1;
        }
        if (cupleaza(st[vecin])) {
            dr[nod] = vecin;
            st[vecin] = nod;
            return 1;
        }
    }
    return 0;
}

int main() {
    int x, y;
    in >> n >> m >> e;
    for (int i = 1; i <= e; ++i) {
        in >> x >> y;
        g[x].push_back(y);
    }
    do {
        pr = cnt;
        memset(f, 0, sizeof(f));
        for (int i = 1; i <= n; ++i) {
            if (!dr[i])
                cnt += cupleaza(i);
        }
    } while (cnt != pr);
    out << cnt << '\n';
    for (int i = 1; i <= n; ++i) {
        if (dr[i])
            out << i << " " << dr[i] << '\n';
    }
    return 0;
}