Cod sursa(job #1698518)

Utilizator DysKodeTurturica Razvan DysKode Data 4 mai 2016 17:44:12
Problema Cuplaj maxim in graf bipartit Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>
#include <list>
#include <bitset>

using namespace std;

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

int a,b,x,y,A[10010],B[10010],i,j,n,m,c;
bitset <10010> fv;
list <int> GA[ 10010 ];

bool cuplaj( int nod )
{
    if( fv[ nod ] == 1 )
        return false;
    fv[ nod ] = 1;
    for( auto it : GA[ nod ] )
        if( B[ it ] == 0 )
        {
            A[ nod ] = it;
            B[ it ] = nod;
            return true;
        }
    for( auto it : GA[ nod ] )
        if( cuplaj( B[ it ] ) )
        {
            A[ nod ] = it;
            B[ it ] = nod;
            return true;
        }
    return false;
}

int main()
{

    fin>>a>>b>>m;
    for( i = 1 ; i <= m ; i++ )
    {
        fin>>x>>y;
        GA[ x ].push_back( y );
    }

    for( i = 1 ; i <= a ; i++ )
    {
        if( A[ i ] == 0 && cuplaj( i ) )
        {
            ++c;
            fv = 0;
        }
    }

    fout<<c<<'\n';
    for( i = 1 ; i <= a ; i++ )
        if( A[ i ] )
            fout<<i<<' '<<A[ i ]<<'\n';

return 0;
}