Cod sursa(job #1496177)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 4 octombrie 2015 15:33:14
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include<fstream>
#include<vector>
#include<bitset>
#define DIM 10015
using namespace std;
int n, m, e, i, j, sol, ok, x, y;
vector<int> v[DIM];
bitset<DIM> viz;
int a[DIM], b[DIM];
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int cupleaza(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){
            a[nod] = vecin;
            b[vecin] = nod;
            sol++;
            return 1;
        }
    }

    for(int i = 0; i < v[nod].size(); i++){
        int vecin = v[nod][i];
        if(cupleaza(b[vecin])){
            a[nod] = vecin;
            b[vecin] = nod;
            return 1;
        }
    }
    return 0;
}
int main(){
    fin>> n >> m >> e;
    for(i = 1; i <= e; i++){
        fin>> x >> y;
        v[x].push_back(y);
    }
    do{
        ok = 0;
        viz.reset();
        for(i = 1; i <= n; i++){
            if(a[i] == 0){
                ok = ok || cupleaza(i);
            }
        }
    }while(ok == 1);

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