Cod sursa(job #1650619)

Utilizator DysKodeTurturica Razvan DysKode Data 11 martie 2016 19:28:34
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
#include <vector>

using namespace std;

vector <int> G[10010];
int a,b,c,A[10010],B[10010],i,j,n,m,x,y,ans;
char f[3000];

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

bool cuplaj( int nod )
{
    if( f[ ( nod - 1 ) / 8 ] & ( 1<<( (nod - 1) % 8 ) ) )
        return false;
    f[ ( nod - 1 ) / 8 ] += ( 1<<( ( nod - 1 ) % 8 ) );
    for( vector<int>::iterator it = G[ nod ].begin() ; it != G[ nod ].end() ; it++ )
    {
        if( B[ *it ] == 0 )
        {
            A[ nod ] = *it;
            B[ *it ] = nod;
            return true;
        }
    }
    for( vector<int>::iterator it = G[ nod ].begin() ; it != G[ nod ].end() ; it++ )
    {
        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;
        G[ x ].push_back( y );
    }

    for( i = 1 ; i <= a ; i++ )
        if( A[ i ] == 0 && cuplaj( i ) )
        {
            ++ans;
            for( j = 0 ; j <= a / 8 + 1 ; j++ )
                f[ j ] = 0;
        }
    fout<<ans<<'\n';
    for( i = 1 ; i <= a ; i++ )
        if( A[ i ] )
            fout<<i<<' '<<A[ i ]<<'\n';

return 0;
}