Cod sursa(job #2679352)

Utilizator ElektrykT E S L A P E F E L I E Elektryk Data 30 noiembrie 2020 13:25:48
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 10137;

bool cuplaj ( int nod );

int n, m, e;

vector <int> g[NMAX];

int x, y;

bool ok;

int R[NMAX];
int L[NMAX];

int viz[NMAX];

int sorin;

int main()
{
    in >> n >> m >> e;
    for ( int i = 1 ; i <= e ; ++i )
    {
        in >> x >> y;
        g[x].push_back (y);
    }
    ok = true;
    while ( ok )
    {
        ok = false;
        memset (viz, 0, sizeof (viz) );
        for ( int i = 1 ; i <= n ; ++i )
            if ( !L[i]  &&  cuplaj (i) )
            {
                ++sorin;
                ok = true;
            }
    }
    out << sorin << '\n';
    for ( int i = 1 ; i <= n ; ++i )
        if ( L[i] )
            out << i << " " << L[i] << '\n';
    return 0;
}

bool cuplaj ( int nod )
{
    if ( viz[nod] )
        return false;
    viz[nod] = 1;
    for ( auto i : g[nod] )
        if ( !R[i]  ||  cuplaj (R[i]) )
        {
            R[i] = nod;
            L[nod] = i;
            return true;
        }
    return false;
}