Cod sursa(job #2663134)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 25 octombrie 2020 14:23:09
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 10005;
int n, m, e, viz[nmax], l[nmax], r[nmax];
vector <int> G[nmax];
bool pairup(int nod){
    if (viz[nod]) return false;
    viz[nod] = true;
    for (auto it : G[nod]){
        if (r[it] == 0){
            l[nod] = it;
            r[it] = nod;
            return true;
        }
    }
    for (auto it : G[nod]){
        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 cuplaj = 0;
    for (int i = 1; i <= n; ++i) if (l[i]) ++cuplaj;
    fout << cuplaj << "\n";
    for (int i = 1; i <= n; ++i) if (l[i]) fout << i << " " << l[i] << "\n";
    fin.close();
    fout.close();
    return 0;
}