Cod sursa(job #2576135)

Utilizator robx12lnLinca Robert robx12ln Data 6 martie 2020 17:29:19
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include<bits/stdc++.h>
using namespace std;

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

const int DIM = 1e4 + 5;

int N, M, E, L[DIM], R[DIM], K;
vector<int> edge[DIM];
bool f[DIM], ok;

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

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

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

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