Cod sursa(job #2409651)

Utilizator robx12lnLinca Robert robx12ln Data 19 aprilie 2019 12:31:46
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

const int DIM = 1e4 + 5;

int f[DIM], L[DIM], R[DIM], N1, N2, M, ans;
vector<int> edge[DIM];

bool match( int nod ){
    if( f[nod] == 1 )
        return false;
    f[nod] = 1;
    for( int i = 0; i < edge[nod].size(); i++ ){
        if( R[ edge[nod][i] ] == 0 ){
            ans++;
            L[nod] = edge[nod][i];
            R[ edge[nod][i] ] = nod;
            return true;
        }
    }
    for( int i = 0; i < edge[nod].size(); i++ ){
        if( match( R[ edge[nod][i] ] ) == true ){
            L[nod] = edge[nod][i];
            R[ edge[nod][i] ] = nod;
            return true;
        }
    }
    return false;
}

int main(){

    fin >> N1 >> N2 >> M;
    for( int i = 1; i <= M; i++ ){
        int x, y; fin >> x >> y;
        edge[x].push_back( y );
    }

    bool ok = true;
    ans = 0;
    while( ok == true ){
        memset( f, 0, sizeof(f) );
        ok = false;
        for( int i = 1; i <= N1; i++ ){
            if( L[i] == 0 && match( i ) == true )
                ok = true;
        }
    }

    fout << ans << "\n";
    for( int i = 1; i <= N1; i++ ){
        if( L[i] != 0 )
            fout << i << " " << L[i] << "\n";
    }
    return 0;
}