Cod sursa(job #1757289)

Utilizator blatulInstitutul de Arta Gastronomica blatul Data 14 septembrie 2016 20:01:44
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <bitset>
using namespace std;

#define Nmax 10002

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

int N, M;
int Left_to_Right[Nmax], Right_to_Left[Nmax];
vector < int > G[Nmax];
bitset < Nmax > Used;

bool PairUP( int node ){

    if( Used[node] )
        return 0;

    vector < int > :: iterator it;

    Used[node] = 1;

    for( it = G[node].begin(); it != G[node].end(); ++it )
        if( !Right_to_Left[*it] ){
            Left_to_Right[node] = *it;
            Right_to_Left[*it] = node;
            return 1;
        }

    for( it = G[node].begin(); it != G[node].end(); ++it )
        if( PairUP( Right_to_Left[*it] ) ){
            Left_to_Right[node] = *it;
            Right_to_Left[*it] = node;
            return 1;
        }

    return 0;

}

int main(){

    int N, M, E;

    fin >> N >> M >> E;

    int x, y;
    while( E-- ){
        fin >> x >> y;
        G[x].push_back(y);
    }

    int Changes = 1;

    while( Changes ){

        Changes = 0;
        Used.reset();

        for( int i = 1; i <= N; ++i )
            if( !Left_to_Right[i] )
                Changes += PairUP(i);
    }

    int Matches = 0;

    for( int i = 1; i <= N; ++i )
        if( Left_to_Right[i] )
            Matches++;

    fout << Matches << "\n";

    for( int i = 1; i <= N; ++i )
        if( Left_to_Right[i] )
            fout << i << " " << Left_to_Right[i] << "\n";

    return 0;
}