Cod sursa(job #412013)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 5 martie 2010 12:05:02
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.82 kb
#include <algorithm>
#include <bitset>
#include <vector>
using namespace std;

#define MAX_N 10005

char s[ MAX_N ];
int N, M, E;
int cnt_s, cuplaj, l[ MAX_N ], r[ MAX_N ];
bitset <MAX_N> f;
vector <int> v[ MAX_N ];

int pair_up( const int &nod ) {

    vector <int> :: iterator it;

    if( f[ nod ] == true )
        return 0;

    f[ nod ] = true;

    for( it = v[ nod ].begin(); it != v[ nod ].end(); ++it )
        if( !r[ *it ] ) {

            l[ nod ] = *it;
            r[ *it ] = nod;

            return 1;
        }

    for( it = v[ nod ].begin(); it != v[ nod ].end(); ++it )
        if( pair_up( r[ *it ] ) ) {

            l[ nod ] = *it;
            r[ *it ] = nod;

            return 1;
        }

    return 0;
}

void read( int &val ) {

    while( !isdigit( s[ cnt_s ] ) )
        if( ++cnt_s == MAX_N ) {

            fread( s, 1, MAX_N, stdin );
            cnt_s = 0;
        }

    val = 0;
    while( isdigit( s[ cnt_s ] ) ) {

        val = val * 10 + s[ cnt_s ] - '0';
        if( ++cnt_s == MAX_N ) {

            fread( s, 1, MAX_N, stdin );
            cnt_s = 0;
        }
    }
}

int main() {

    freopen( "cuplaj.in", "r", stdin );
    freopen( "cuplaj.out", "w", stdout );

    int i, x, y, ok;

    cnt_s = MAX_N - 1;

    read( N );
    read( M );
    read( E );

    while( E-- ) {

        read( x );
        read( y );

        v[ x ].push_back( y );
    }

    do {

        ok = 0;
        f.reset();

        for( i = 1; i <= N; ++i )
            if( !l[ i ] && pair_up( i ) ) {

                ++cuplaj;
                ok = 1;
            }
    }
    while( ok );

    printf( "%d\n", cuplaj );
    for( i = 1; i <= N; ++i )
        if( l[ i ] )
            printf( "%d %d\n", i, l[ i ] );

    return 0;
}