Cod sursa(job #2929713)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 26 octombrie 2022 17:31:21
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

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

const int max_size = 1e4 + 1;

vector <int> mc[max_size];
int st[max_size], dr[max_size];
bitset <max_size> viz;

bool pairup (int nod)
{
    if (viz[nod])
    {
        return false;
    }
    viz[nod] = true;
    for (auto f : mc[nod])
    {
        if (dr[f] == 0)
        {
            /// nod in dr necuplat
            st[nod] = f;
            dr[f] = nod;
            return true;
        }
    }
    for (auto f : mc[nod])
    {
        if (pairup(dr[f]))
        {
            /// am gasit nod pt cuplarea noua
            st[nod] = f;
            dr[f] = nod;
            return true;
        }
    }
    return false;
}

int main ()
{
    int l, r, m;
    in >> l >> r >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y;
        in >> x >> y;
        mc[x].push_back(y);
    }
    while (1)
    {
        for (int i = 1; i <= l; i++)
        {
            viz[i] = 0;
        }
        bool ok = false;
        for (int i = 1; i <= l; i++)
        {
            if (!st[i])
            {
                ok |= pairup(i);
            }
        }
        if (!ok)
        {
            break;
        }
    }
    int max1 = 0;
    for (int i = 1; i <= l; i++)
    {
        if (st[i] > 0)
        {
            max1++;
        }
    }
    out << max1 << '\n';
    for (int i = 1; i <= l; i++)
    {
        if (st[i] > 0)
        {
            out << i << " " << st[i] << '\n';
        }
    }
    in.close();
    out.close();
    return 0;
}