Cod sursa(job #1922906)

Utilizator DysKodeTurturica Razvan DysKode Data 10 martie 2017 19:33:52
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second

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

int A[10010],B[10010],i,j,n,m,fv[10010],x,y,ans,q;
list< int > G[ 10010 ];

bool cuplaj( int nod )
{
    if( fv[ nod / 30 ] & ( 1 << ( nod % 30 ) ) )
        return false;
    fv[ nod / 30 ] += 1 << ( nod % 30 );
    for( auto it : G[ nod ] )
    {
        if( !B[ it ] )
        {
            A[ nod ] = it;
            B[ it ] = nod;
            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 ] )
        {
            for( j = 0 ; j <= n / 30 + 5 ; j++ )
                fv[ j ] = 0;
            if( cuplaj( i ) )
            {
                ++ans;
            }
        }
    }
    fout<<ans<<'\n';
    for( i = 1 ; i <= n ; i++ )
        if( A[ i ] )
            fout<<i<<' '<<A[ i ]<<'\n';

return 0;
}