Cod sursa(job #2603921)

Utilizator MocalinnoMoca Andrei Catalin Mocalinno Data 21 aprilie 2020 12:05:30
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <bits/stdc++.h>
#define DAU  ios::sync_with_stdio(false); fin.tie(0); fout.tie(0);
#define PLEC fin.close(); fout.close(); return 0;
using namespace std;
const string problem("cuplaj");
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");
using VB  = vector<bool>;
using VI  = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using VP  = vector<PII>;
VVI g;
VB viz;
VP sol;
VI leftmatch, rightmatch;
int n, m, k, x, y;
bool tinder(true);
inline bool Match(int x) {
    if (viz[x])
        return false;
    viz[x] = true;
    for (const int& y : g[x])
        if (!leftmatch[y] || Match(leftmatch[y])) {
            leftmatch[y] = x, rightmatch[x] = y;
            return true;
        }
    return false;
}
int main() {
    DAU
    fin >> n >> m >> k;
    g = VVI(n + 1);
    for (int i = 1; i <= k; ++i) {
        fin >> x >> y;
        g[x].emplace_back(y);
    }
    leftmatch = VI(m + 1);
    rightmatch = VI(n + 1);
    viz = VB(n + 1);
    while (tinder) {
        tinder = false;
        fill(viz.begin(), viz.end(), false);
        for (int i = 1; i <= n; ++i)
            if (!rightmatch[i] && Match(i))
                tinder = true;
    }
    for (int i = 1; i <= n; ++i)
        if (rightmatch[i])
            sol.emplace_back(i, rightmatch[i]);
    fout << sol.size() << '\n';
    for (const PII& P : sol)
        fout << P.first << ' ' << P.second << '\n';
    PLEC
}