Cod sursa(job #1922914)

Utilizator DysKodeTurturica Razvan DysKode Data 10 martie 2017 19:35:59
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,x,y,ans,q;
char fv[10010];
list< int > G[ 10010 ];

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 ] )
        {
            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 / 8 + 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;
}