Cod sursa(job #2873515)

Utilizator alex_braslasuBraslasu Alexandru alex_braslasu Data 19 martie 2022 10:48:10
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 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, 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;
    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()
{
    for (int i = 1; i <= n; ++i)
        if (!st[i])
        {
            if (cupleaza(i))
                ++nr;
            else
            {
                for (int j = 1; j <= n; ++j)
                    viz[j] = 0;
                if (cupleaza(i))
                    ++nr;
            }
        }
}

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();
    g << nr << '\n';
    for (i = 1; i <= n; ++i)
        if (st[i])
            g << i << " " << st[i] << '\n';
    return 0;
}