Cod sursa(job #2188271)

Utilizator circeanubogdanCirceanu Bogdan circeanubogdan Data 27 martie 2018 01:33:23
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#include <vector>
#define DIM 10002

using namespace std;

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

int n, m, Left[DIM], Right[DIM], muc, x, y, cuplaj_max;

bitset<DIM> viz;

vector<int> graf[DIM];

vector<pair<int, int> > sol;

bool cuplaj(int nod){
    if(viz[nod] == 1)
        return false;
    viz[nod] = true;
    for(auto nodc : graf[nod]){
        if(Right[nodc] == 0){
            Right[nodc] = nod;
            Left[nod] = nodc;
            return true;
        }
    }
    for(auto nodc : graf[nod]){
        if(cuplaj(Right[nodc])){
            Right[nodc] = nod;
            Left[nod] = nodc;
            return true;
        }
    }
    return false;
}

int main(int argc, const char * argv[]) {
    in>>n>>m>>muc;
    for(int i = 1; i <= muc; ++ i){
        in>>x>>y;
        graf[x].push_back(y);
    }
    int ok = 1;
    while(ok){
        ok = 0;
        viz.reset();
        for(int i = 1; i <= n; ++ i)
            if(!Left[i])
                ok |= cuplaj(i);
    }
    
    for(int i = 1; i <= n; ++ i)
        if(Left[i])
            sol.push_back(make_pair(i, Left[i]));
    
    out<<sol.size()<<'\n';
    
    for(auto it : sol)
        out<<it.first<<" "<<it.second<<'\n';
    
    return 0;
}