Cod sursa(job #2207213)

Utilizator Dobricean_IoanDobricean Ionut Dobricean_Ioan Data 25 mai 2018 10:45:52
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <vector>
#include <fstream>
#include <cstring>

using namespace std;

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

const int Dim = 10001;

using VI = vector < int > ;
using VVI = vector < VI > ;

int n,m,e,maxmat,L[Dim],R[Dim];
bool V[Dim];
VVI G;
void Read();
void HopcroftKarp();
bool Cupleaza(int x);

int main() {

    Read();
    HopcroftKarp();
    fout << maxmat << "\n";
    for ( int i = 1; i <= n; ++i)
        if (L[i])
            fout << i << " " << L[i] << "\n";
}


void  HopcroftKarp() {

    bool found_path = true;
    while (found_path) {
        found_path = false;
        memset(V,0,sizeof(V));
        for ( int i = 1; i <= n; ++i)
            if (!L[i] and Cupleaza(i))
                ++maxmat, found_path = true;
        }
}

bool Cupleaza(int x) {

    if ( V[x] ) return false;
    V[x] = true;
    for ( const int & y : G[x])
        if (!R[y] or Cupleaza(R[y])) {
                L[x] = y;
                R[y] = x;
                return true;
            }

    return false;
}


void Read() {

    fin >> n >> m >> e;
    G = VVI(n+1);
    int x,y;
    for (; e > 0; --e) {
        fin >> x >> y;
        G[x].push_back(y);
        }
}