Cod sursa(job #2503088)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 2 decembrie 2019 12:40:04
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>

using namespace std;

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

const int Nmax = 10000;

vector <int> g[Nmax + 5];
int n1, n2, m, l[Nmax + 5], r[Nmax + 5];
bool use[Nmax + 5];

void Read(){
    fin >> n1 >> n2 >> m;
    for (int i = 1; i <= m; i++){
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
    }
}

int Pairup(int nod){
    if (use[nod])
        return 0;
    use[nod] = 1;
    for (auto i : g[nod])
        if (!r[i]){
            r[i] = nod;
            l[nod] = i;
            return 1;
        }
    for (auto i : g[nod])
        if (Pairup(r[i])){
            r[i] = nod;
            l[nod] = i;
            return 1;
        }
    return 0;
}

void Solve(){
    for (int i = 1; i <= n1; i++){
        memset(use, 0, sizeof use);
        Pairup(i);
    }
}

void Print(){
    int nr = 0;
    for (int i = 1; i <= n1; i++)
        if (l[i])
            nr++;
    fout << nr << '\n';
    for (int i = 1; i <= n1; i++)
        if (l[i])
            fout << i << ' ' << l[i] << '\n';
}

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