Cod sursa(job #2502582)

Utilizator robx12lnLinca Robert robx12ln Data 1 decembrie 2019 10:18:37
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include<bits/stdc++.h>
using namespace std;

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

const int DIM = 1e4 + 5;

int L[DIM], R[DIM], N, M, nr, sol, f[DIM];
bool ok = true;
vector<int> edge[DIM];

bool match( int nod ){
    if( f[nod] == 1 )
        return false;
    f[nod] = 1;
    for( int i = 0; i < edge[nod].size(); i++ ){
        int vecin = edge[nod][i];
        if( R[vecin] == 0 ){
            sol++;
            L[nod] = vecin;
            R[vecin] = nod;
            return true;
        }
    }
    for( int i = 0; i < edge[nod].size(); i++ ){
        int vecin = edge[nod][i];
        if( match( R[vecin] ) == true ){
            L[nod] = vecin;
            R[vecin] = nod;
            return true;
        }
    }
    return false;
}

int main(){
    fin >> N >> M >> nr;
    for( int i = 1; i <= nr; i++ ){
        int x, y; fin >> x >> y;
        edge[x].push_back( y );
    }

    sol = 0;
    while( ok ){
        ok = false;
        memset( f, 0, sizeof(f) );
        for( int i = 1; i <= N; i++ )
            if( L[i] == 0 && match( i ) == true )
                ok = true;
    }

    fout << sol << "\n";
    for( int i = 1; i <= N; i++ )
        if( L[i] != 0 )
            fout << i << " " << L[i] << "\n";
    return 0;
}