Cod sursa(job #2736125)

Utilizator Iulia25Hosu Iulia Iulia25 Data 3 aprilie 2021 10:45:43
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream cin ("cuplaj.in");
ofstream cout ("cuplaj.out");

const int N = 1e4 + 5;

int viz[N], l[N], r[N];

vector <int> v[N];

bool dfs(int nod)   {
    viz[nod] = true;
    for (auto it : v[nod])  {
        if (!l[it] || (!viz[l[it]] && dfs(l[it])))  {
            l[it] = nod;
            r[nod] = it;
            return true;
        }
    }
    return false;
}

int main()  {
    int n, m, e, x, y, cnt = 0;
    cin >> n >> m >> e;
    for (int i = 1; i <= e; ++i)    {
        cin >> x >> y;
        v[x].push_back(y);
    }
    bool ok = true;
    while (ok)  {
        ok = false;
        for (int i = 1; i <= n; ++i)
            viz[i] = false;
        for (int i = 1; i <= n; ++i)    {
            if (!r[i])  {
                if (dfs(i)) {
                    ++cnt;
                    ok = true;
                }
            }
        }
    }
    cout << cnt << '\n';
    for (int i = 1; i <= n; ++i)    {
        if (r[i])
            cout << i << ' ' << r[i] << '\n';
    }
    return 0;
}