Cod sursa(job #2140076)

Utilizator DawlauAndrei Blahovici Dawlau Data 22 februarie 2018 23:50:20
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include<fstream>
#include<bitset>
#include<list>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int NMAX = 1e4 + 5;

bool used[NMAX];
list<int>graph[NMAX];

int leftMatch[NMAX], rightMatch[NMAX];
int leftNodes, rightNodes, edgesCount;


inline void readData(){

    fin >> leftNodes >> rightNodes >> edgesCount;

    int from, to;

    while(edgesCount--){

        fin >> from >> to;
        graph[from].push_back(to);
    }
}

bool match(int node){

    if(used[node])
        return false;

    used[node] = true;

    for(const auto &nextNode:graph[node])
        if(!leftMatch[nextNode] or match(leftMatch[nextNode])){

            rightMatch[node] = nextNode;
            leftMatch[nextNode] = node;
            return true;
        }

    return false;
}

inline void getMaxMatching(){

    bool possibleMatching = true;
    int node;

    while(possibleMatching){

        possibleMatching = false;
        fill(used + 1, used + 1 + leftNodes, false);

        for(node = 1; node <= leftNodes; ++node)
            if(!rightMatch[node])
                if(match(node))
                    possibleMatching = true;
    }
}

inline void printMaxMatching(){

    int node, matching = 0;

    for(node = 1; node <= leftNodes; ++node)
        if(rightMatch[node])
            ++matching;

    fout << matching << '\n';

    for(node = 1; node <= leftNodes; ++node)
        if(rightMatch[node])
            fout << node << ' ' << rightMatch[node] << '\n';
}

int main(){

    readData();
    getMaxMatching();
    printMaxMatching();
}