Cod sursa(job #2664954)

Utilizator mariamirabella2Bucur-Sabau Maria-Mirabela mariamirabella2 Data 29 octombrie 2020 19:49:18
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <vector>
#include <fstream>
#include <cstring>

using namespace std;

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

const int MAXN = 1e5;
vector < int > g[MAXN];
int L[MAXN], viz[MAXN]; /// L[i] = cu ce nod din dreapta este cuplat nodul din stanga i
int R[MAXN]; /// R[i] = cu ce nod din stanga este cuplat nodul i din dreapta
int n,m,e,x,y;
bool ok;

bool match(int nod) { ///daca pot sa cuplez nodul din stanga nod
    if(viz[nod])
        return false;
    viz[nod]=1;
    for (auto y : g[nod])
    if(!R[y] or match(R[y])) {
        L[nod] = y;
        R[y] = nod;
        return true;
    }

    return false;
}
int cnt=0;
int main() {

    cin >> n >> m >>e;
    for ( int i = 1;  i<= e; ++i) {
        cin>> x >> y;
        g[x].push_back(n+y);
    }

        ok = true;
        while(ok == true) { /// cat timp am mai cuplat si eu ceva o iulia un cayuss un madalina
                ok = false;
                memset(viz,0,sizeof(viz));
            for ( int i =1; i <= n; ++i)
                    if(!L[i] and match(i) == true)
            {
                ++cnt;
                ok = true;
            }
        }
        cout << cnt << '\n';
        for ( int i = 1; i <= n; ++i)
                if(L[i])
                cout << i << " " << L[i]-n<<'\n';

}