Cod sursa(job #2489305)

Utilizator Dragos1226Dragos Chileban Dragos1226 Data 8 noiembrie 2019 14:07:19
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include<fstream>
#include<vector>
#include<cstring>
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
const int NMax = 10000;
vector <int> G[NMax+5];

int L[NMax+5], R[NMax+5], N, M, E, Use[NMax+5];

void Read() {
    in >> N >> M >> E;
    for (int i = 1; i <= E; i++) {
        int x, y;
        in >> x >> y;
        G[x].push_back(y);
    }
}

bool Pairup(int Nod) {
    if(Use[Nod]) return 0;
    Use[Nod] = 1;
    for (auto Vecin : G[Nod]) {
        if(!R[Vecin]) {
            L[Nod] = Vecin;
            R[Vecin] = Nod;
            return 1;
        }
    }
    for (auto Vecin : G[Nod]) {
        if(Pairup(R[Vecin])) {
            L[Nod] = Vecin;
            R[Vecin] = Nod;
            return 1;
        }
    }
    return 0;
}

void Solve() {
    bool ok;

    do {
        ok = 0;memset(Use,0,sizeof(Use));

        for (int i = 1; i <= N; i++)
           if(!L[i])
            ok |= Pairup(i);
    }
    while(ok);
}

void Print() {
    int Sol = 0;
    for (int i = 1; i <= N; i++)
        if(L[i]) Sol++;
    out << Sol << '\n';
    for (int i = 1; i <= N; i++)
        if(L[i])
            out << i << " " << L[i] << '\n';
}

int main() {
    Read();
    Solve();
    Print();
}