Cod sursa(job #1923377)

Utilizator DysKodeTurturica Razvan DysKode Data 10 martie 2017 23:16:06
Problema Cuplaj maxim in graf bipartit Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;

#define f first
#define s second

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

int a[2010],b[2010],i,j,n,m,fv[1010],q,x,y,ans;
vector< int > G[ 2010 ];

bool cuplaj( int nod )
{
    if( fv[ nod / 8 ] & 1 << ( nod % 8 ) )
        return false;
    fv[ nod / 8 ] += 1<<( nod %8 );
    for( auto it : G[ nod ] )
    {
        if( !b[ it ] )
        {
            b[ it ] = nod;
            a[ nod ] = it;
            return true;
        }
    }

    for( auto it : G[ nod ] )
    {
        if( cuplaj( b[ it ] ) )
        {
            b[ it ] = nod;
            a[ nod ] = it;
            return true;
        }
    }
    return false;
}

int main()
{
    fin>>n>>m>>q;
    for( i = 1 ; i <= q ; i++ )
    {
        fin>>x>>y;
        G[ x ].push_back( y );
    }

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

return 0;
}