Cod sursa(job #582269)

Utilizator BitOneSAlexandru BitOne Data 15 aprilie 2011 09:53:40
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bitset>
#include <vector>
#include <fstream>
#include <cstdlib>
#define N_MAX 10011

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