Cod sursa(job #2886326)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 7 aprilie 2022 16:34:52
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x <<": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "cuplaj";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");

const int mxn = 100005;

int n, m, e;
int st[10005];
int dr[10005];
bitset < 10005 > viz;
vector<vector<int> > g;
bool cuplaj(int nod) {

    if (viz[nod])
        return false;
    viz[nod] = true;
    for (int i : g[nod]) {
        if (!dr[i]) {
            st[nod] = i;
            dr[i] = nod;

            return true;
        }
    }

    for (int i : g[nod]) {
        if (cuplaj(dr[i])) {
            st[nod] = i;
            dr[i] = nod;
            return true;
        }
    }

    return false;
}


int main() {

    fin >> n >> m >> e;
    g = vector<vector<int> >(max(n, m) + 1);
    while (e--) {
        int x, y;
        fin >> x >> y;
        g[x].pb(y);
    }

    bool ok = false;
    while (!ok) {
        ok = true;
        viz.reset();
        for (int i = 1; i <= n; ++i) {
            if (!st[i] && cuplaj(i)) {
                ok = false;
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        if (st[i])
            ans++;
    }
    fout << ans << '\n';
    for (int i = 1; i <= n; ++i)
        if (st[i])
            fout << i << " " << st[i] << '\n';

    return 0;
}