Cod sursa(job #921446)

Utilizator paul_gabryelPaul Buda paul_gabryel Data 20 martie 2013 23:36:15
Problema Cuplaj maxim in graf bipartit Scor 28
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb

#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

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

const int N = 10001;

int n,m,L[N],R[N],SOL;
vector<int> G[N];
bool UZ[N];

void READ ()
{

    in>>n>>m>>m;

    for( int x,y ; m ; --m )
    {

        in>>x>>y;

        G[x].push_back(y);

    }

}

bool PAIRUP ( int nod )
{

    if( UZ[nod] )
        return 0;

    UZ[nod]=1;

    for( vector<int>::iterator it=G[nod].begin() ; it < G[nod].end() ; ++it )
        if( !R[*it] )
        {

            R[*it]=nod;
            L[nod]=*it;

            ++SOL;

            return 1;

        }

    for( vector<int>::iterator it=G[nod].begin() ; it < G[nod].end() ; ++it )
        if( PAIRUP(*it) )
        {

            R[*it]=nod;
            L[nod]=*it;

            return 1;

        }


    return 0;

}

void SOLVE ()
{

    for( bool ok=1 ; ok ; )
    {

        ok=0;

        memset(UZ,0,sizeof(UZ));

        for( int i=1 ; i <= n ; ++i )
            if( !L[i] && PAIRUP(i) )
                ok=1;

    }

}

void PRINT ()
{

    out<<SOL<<'\n';

    for( int i=1 ; i <= n ; ++i )
        if( L[i] )
            out<<i<<' '<<L[i]<<'\n';

}

int main ()
{

    READ ();
    SOLVE ();
    PRINT ();

    return 0;

}