Cod sursa(job #2950252)

Utilizator brianna_enacheEnache Brianna brianna_enache Data 3 decembrie 2022 13:00:24
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");

vector <int> a[100005];
bitset <10005> viz;
int m, n, e, st[100005], dr[100005], sol;

int 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] = 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]) == 1)
    {
        /// cuplez nod_2
        st[nod] = nod_2;
        dr[nod_2] = nod;
        return 1;
    }

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

void Test_Case()
{
    int i, ok, x, y;
    in >> n >> m >> e;
    while(e--)
    {
        in >> 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))
        {
            sol++;
            ok = 0;
        }
    }
    out << sol << "\n";
    for(i = 1; i <= n; i++)
        if(st[i] != 0)
          out << i << " " << st[i] << "\n";
}
int main()
{
    Test_Case();
    return 0;
}