Cod sursa(job #2874119)

Utilizator alex_braslasuBraslasu Alexandru alex_braslasu Data 19 martie 2022 12:54:52
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <vector>

#define N_MAX 10010

using namespace std;

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

int n, m, n1, n2, e, i, x, y, nr, ok, st[N_MAX], dr[N_MAX], viz[N_MAX];
vector<int > G[N_MAX];

static inline int cupleaza(int nod)
{
    if (viz[nod])
        return 0;
    else
    {
        viz[nod] = 1;
        for (auto it : G[nod])
            if (!dr[it] || cupleaza(dr[it]))
            {
                st[nod] = it;
                dr[it] = nod;
                return 1;
            }
        return 0;

    }
}

static inline void cuplaj()
{
    ok = 1;
    while (ok)
    {
        for (i = 1; i <= n; ++i)
            viz[i] = 0;
        ok = 0;
        for (i = 1; i <= n; ++i)
            if (!st[i] && cupleaza(i))
                ok = 1;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    f.tie(nullptr);
    f >> n >> m >> e;
    for (i = 1; i <= e; ++i)
    {
        f >> x >> y;
        G[x].push_back(y);
    }
    cuplaj();
    for (i = 1; i <= n; ++i)
        if (st[i])
            ++nr;
    g << nr << '\n';
    for (i = 1; i <= n; ++i)
        if (st[i])
            g << i << " " << st[i] << '\n';
    return 0;
}