Cod sursa(job #1165991)

Utilizator roots4Irimia Alexandru Gabriel roots4 Data 3 aprilie 2014 09:09:01
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include<fstream>
#include<vector>
#include<string.h>

using namespace std;

#define max_n 10010

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

vector<int>L[max_n];
int n , m , e , x , y , c;
int st[max_n];
int dr[max_n];
bool Viz[max_n] , ok = true;

void read(){

    f>>n>>m>>e;

    for( int i = 1 ; i <= e ; i++ ){
        f>>x>>y;
        L[x].push_back(y);
    }

}

int cuplaj( int nod ){

    if( Viz[nod] )
        return 0;

    Viz[nod] = 1;

    for( unsigned int i = 0 ; i < L[nod].size() ; i++ ){
        if( dr[L[nod][i]] == 0 ){
            st[nod] = L[nod][i];
            dr[L[nod][i]] = nod;
            return 1;
        }
    }

    for( unsigned int i = 0 ; i < L[nod].size() ; i++ ){
        if( cuplaj( dr[L[nod][i]] ) ){
            st[nod] = L[nod][i];
            dr[L[nod][i]] = nod;
            return 1;
        }
    }

    return 0;

}

void print(){

    for( int i = 1 ; i <= n ; i++ )
        if( st[i] )
            c++;

    g<<c<<"\n";

    for( int i = 1 ; i <= n ; i++ )
        if( st[i] )
            g<<i<<" "<<st[i]<<"\n";

}

int main(){

    read();

    while( ok ){

        memset( Viz , 0  , sizeof(Viz) );   ok = false;

        for( int i = 1 ; i <= n ; i++ ){
            if( st[i] )
                continue;
            if( cuplaj(i) )
                ok = true;
        }

    }

    print();

    return 0;
}