Cod sursa(job #648521)

Utilizator vendettaSalajan Razvan vendetta Data 13 decembrie 2011 17:11:37
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

const int nmax = 10001;

vector<int> gf[nmax];
int st[nmax], dr[nmax];
bitset<nmax> viz;
int n, m, e;
int n_cup;

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

void citeste(){

    f>>n>>m>>e;

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

}

bool cupleaza(int nod){

    if (viz[nod]!=0) return 0;
    viz[nod] = 1;

    for(unsigned i=0; i < gf[nod].size(); ++i){
        int vc = gf[nod][i];
        if (dr[vc] == 0 || cupleaza( dr[vc] ) != 0){
            st[nod] = vc;
            dr[vc] = nod;
            return 1;
        }
    }

    return 0;

}

void rezolva(){

    int ok = 1;

    for(; ok; ){
        ok = 0;
        for(int i=1; i<=nmax; ++i) viz[i] = 0;
        for(int i=1; i<=n; ++i){
            if (st[i] == 0 && cupleaza(i) ){
                ok = 1;
                ++n_cup;
            }
        }
    }

}

void scrie(){

    g<<n_cup<<"\n";
    for(int i=1; i<=n; ++i){
        if (st[i]!=0){
            g<<i<<" "<<st[i]<<"\n";
        }
    }

}

int main(){

    citeste();
    rezolva();
    scrie();

    f.close();
    g.close();

    return 0;

}