Cod sursa(job #2948887)

Utilizator tomaionutIDorando tomaionut Data 28 noiembrie 2022 18:26:15
Problema Cuplaj maxim in graf bipartit Scor 28
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m, e, st[10005], dr[10005], sol;
bitset <100005> viz;
vector <int> a[10005];

bool Cuplaj(int nod)
{
    if (viz[nod] == 1)
        return 0;

    viz[nod] = 1;
    for (auto nod_2 : a[nod])
    {
        if (dr[nod_2] == 0) // daca adiacentul nu este cuplat
        {
            /// cuplez nod_2
            st[nod] = nod_2;
            dr[nod_2] = st[nod];
            return 1;
        }
    }

    /// daca ajung aici, toti adiacentii lui nod sunt deja cuplati
   /// incerc sa "decuplez" fiecare dintre adiacentii lui nod
   /// repetand aceeasi procedura
    
    for (auto nod_2 : a[nod])
    {
        if (Cuplaj(dr[nod_2])) 
        {
            /// cuplez nod_2
            st[nod] = nod_2;
            dr[nod_2] = st[nod];
            return 1;
        }
    }

    /// daca ajung aici nu se mai pot realiza cuplaje
    return 0;
    
}

void Test_Case()
{
    int x, y, i, ok;
    fin >> n >> m >> e;
    while (e--)
    {
        fin >> x >> y;
        a[x].push_back(y);
    }

    ok = 0;
    while (ok == 0)
    {
        ok = 1;
        viz.reset();
        for (i = 1; i <= n; i++)
            if (st[i] == 0 and Cuplaj(i))
            {
                ok = 0;
                sol++;
            }
    }

    fout << sol << "\n";
    for (i = 1; i <= n; i++)
        if (st[i] != 0)
            fout << i << " " << st[i] << "\n";
}

int main()
{
    int t;
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    t = 1;
    while (t--)
        Test_Case();

    return 0;
}