Cod sursa(job #460183)

Utilizator BitOneSAlexandru BitOne Data 1 iunie 2010 15:26:48
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <vector>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#define Nmax 10111

/*
 *
 */
using namespace std;
bool was[Nmax];
int L[Nmax], R[Nmax];
vector< int > G[Nmax];
inline bool PairUp( int x )
{
    if( was[x] )
        return false;
    was[x]=true;
    vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
    for( ; it < iend; ++it )
        if( !R[*it] || PairUp( R[*it] ) )
        {
            L[x]=*it;
            R[*it]=x;
            return true;
        }
    return false;
}
int main( void )
{
    bool ok;
    int N, M, E, x, y, nr=0;
    ifstream in( "cuplaj.in" );
    for( in>>N>>M>>E; E; --E )
    {
        in>>x>>y;
        G[x].push_back(y);
    }
    do
    {
        ok=false;
        for( x=1; x <= N; ++x )
            if( !L[x] && PairUp(x) )
                ok=true, ++nr;
        fill( was, was+N+1, false );
    }while( ok );
    ofstream out( "cuplaj.out" );
    out<<nr<<'\n';
    for( x=1; x <= N; ++x )
        if( L[x] )
            out<<x<<' '<<L[x]<<'\n';
    return EXIT_SUCCESS;
}