Cod sursa(job #2409666)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 19 aprilie 2019 12:35:53
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include<fstream>
#include<algorithm>
#include<bitset>
#define DIM 10005
using namespace std;
int n, m, nr, x, y, i, ok, sol;
int a[DIM], b[DIM];
bitset<DIM> viz;
vector<int> v[DIM];
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int cuplaj(int nod){
    if(viz[nod] == 1){
        return 0;
    }
    viz[nod] = 1;
    for(int i = 0; i < v[nod].size(); i++){
        int vecin = v[nod][i];
        if(b[vecin] == 0){
            sol++;
            a[nod] = vecin;
            b[vecin] = nod;
            return 1;
        }
    }
    for(int i = 0; i < v[nod].size(); i++){
        int vecin = v[nod][i];
        if( cuplaj(b[vecin]) ){
            a[nod] = vecin;
            b[vecin] = nod;
            return 1;
        }
    }
    return 0;
}
int main(){
    fin>> n >> m >> nr;
    for(i = 1; i <= nr; i++){
        fin>> x >> y;
        v[x].push_back(y);
    }
    do{
        ok = 0;
        viz.reset();
        for(i = 1; i <= n; i++){
            if(a[i] == 0 && cuplaj(i) ){
                ok = 1;
            }
        }
    }while(ok == 1);

    fout<< sol <<"\n";
    for(i = 1; i <= n; i++){
        if(a[i] != 0){
            fout<< i <<" "<< a[i] <<"\n";
        }
    }
    return 0;
}