Cod sursa(job #1551189)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 15 decembrie 2015 12:16:11
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include<fstream>
#include<vector>
#include<cstring>

using namespace std;

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

typedef vector< int > graf;

const int nmax = 1e4;
bool viz[ nmax + 1 ];
int l[ nmax + 1 ], r[ nmax + 1 ];
graf g[ nmax + 1 ];

int pair_up( int nod ) {
    if ( viz[ nod ] == 1 ) {
        return 0;
    }
    viz[ nod ] = 1;
    for( graf::iterator it = g[ nod ].begin(); it != g[ nod ].end(); ++ it ) {
        if ( l[ *it ] == 0 || pair_up( l[ *it ] ) ) {
            l[ *it ] = nod
            r[ nod ] = *it;
            return 1;
        }
    }
    return 0;
}
int main() {
    int n, m, e;
    fin >> n >> m >> e;
    for( int i = 0; i < e; ++ i ) {
        int x, y;
        fin >> x >> y;
        g[ x ].push_back( y );
    }

    int ans = 0;
    bool ok = 1;
    while( ok == 1 ) {
        memset( viz, 0, sizeof( viz ) );

        ok = 0;
        for( int i = 1; i <= n; ++ i ) {
            if ( r[ i ] == 0 && pair_up( i ) ) {
                ++ ans;
                ok = 1;
            }
        }
    }

    fout << ans << "\n";
    for( int i = 1; i <= n; ++ i ) {
        if ( r[ i ] > 0 ) {
            fout << i << " " << r[ i ] << "\n";
        }
    }

    fin.close();
    fout.close();
    return 0;
}