Cod sursa(job #3304140)

Utilizator LucaMirsolea14Luca Mirsolea LucaMirsolea14 Data 21 iulie 2025 10:39:04
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include<fstream>
#include<vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

const int N = 100002;

int n1, n2, m, vizit[N], ok, p[N];

vector<int> g[N];

bool cupleaza(int nod) {
    if (vizit[nod])
        return false;
    vizit[nod] = 1;
    for (auto e : g[nod]) {
        if (!p[e] || cupleaza(p[e])) {
            p[e] = nod;
            p[nod] = e;
            return true;
        }
    }
    return false;
}

int main() {

    int i, j, x, y, cuplaj = 0;
    fin >> n1 >> n2 >> m;
    for (i = 1; i <= m; i++) {
        fin >> x >> y;
        g[x].push_back(y + n1);
    }
    ok = 1;
    while (ok) {
        ok = 0;
        for (i = 1; i <= n1; i++)
            vizit[i] = 0;
        for (i = 1; i <= n1; i++) {
            if (!p[i] && cupleaza(i)) {
                cuplaj++;
                ok = 1;
            }
        }
    }
    fout << cuplaj << '\n';
    for (i = 1; i <= n1; i++)
        if (p[i])
            fout << i << " " << p[i] - n1 << '\n';
}