Cod sursa(job #2721089)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 11 martie 2021 15:56:05
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 10005;
int n, m, e, l[nmax], r[nmax];
bool viz[nmax];
vector <int> G[nmax];

bool pairup(int nod){
    viz[nod] = true;
    for (auto it : G[nod]){
        if (r[it] == 0){
            l[nod] = it;
            r[it] = nod;
            return true;
        }
    }
    bool ok = false;
    for (auto it : G[nod]){
        if (viz[r[it]] == false){
            if (pairup(r[it])){
                l[nod] = it;
                r[it] = nod;
                return true;
            }
        }
    }
    return false;
}

int main(){
    fin >> n >> m >> e;
    for (int i = 1; i <= e; ++i){
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
    }
    bool ok = true;
    while (ok){
        ok = false;
        memset(viz, 0, sizeof viz);
        for (int i = 1; i <= n; ++i){
            if (l[i] == 0){
                ok |= pairup(i);
            }
        }
    }
    int contor = 0;
    for (int i = 1; i <= n; ++i){
        if (l[i]) ++contor;
    }
    fout << contor << "\n";
    for (int i = 1; i <= n; ++i){
        if (l[i]){
            fout << i << " " << l[i] << "\n";
        }
    }
    fin.close();
    fout.close();
    return 0;
}