Cod sursa(job #3124412)

Utilizator rapidu36Victor Manz rapidu36 Data 28 aprilie 2023 16:28:10
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

const int N = 1e4;
const int M = 1e5;

vector <int> a1[N+1], a2[N+1], pereche1, pereche2;
bitset <N+1> viz;

int caut_pereche(int x1)
{
    if (viz[x1])
    {
        return 0;
    }
    viz[x1] = 1;
    for (auto x2: a1[x1])
    {
        if (pereche2[x2] == 0 || caut_pereche(pereche2[x2]))
        {
            pereche1[x1] = x2;
            pereche2[x2] = x1;
            return x2;
        }
    }
    return 0;
}

int main()
{
    ifstream in("cuplaj.in");
    ofstream out("cuplaj.out");
    int n1, n2, m;
    in >> n1 >> n2 >> m;
    pereche1.resize(n1 + 1, 0);
    pereche2.resize(n2 + 1, 0);
    for (int i = 0; i < m; i++)
    {
        int x1, x2;
        in >> x1 >> x2;
        a1[x1].push_back(x2);
        a2[x2].push_back(x1);
    }
    int cuplaj = 0;
    bool cresc_c;
    do
    {
        cresc_c = false;
        viz.reset();
        for (int x1 = 1; x1 <= n1; x1++)
        {
            if (pereche1[x1] == 0)
            {
                pereche1[x1] = caut_pereche(x1);
                if (pereche1[x1] != 0)
                {
                    cresc_c = true;
                    cuplaj++;
                }
            }
        }
    }
    while (cresc_c);
    out << cuplaj << "\n";
    for (int x1 = 1; x1 <= n1; x1++)
    {
        if (pereche1[x1] != 0)
        {
            out << x1 << " " << pereche1[x1] << "\n";
        }
    }
    in.close();
    out.close();
    return 0;
}