Cod sursa(job #2210244)

Utilizator silviu982001Borsan Silviu silviu982001 Data 5 iunie 2018 22:49:53
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <fstream>
#include <vector>
#include <bitset>

#define NMAX 10001

using namespace std;

vector<int> v[NMAX];
bitset<NMAX> viz;
int n, m, e, cuplaje;
int l[NMAX], r[NMAX];

bool cuplaj(int nod)
{
    if (viz[nod])
        return false;
    viz[nod] = true;
    for (int it : v[nod])
        if (!r[it])
    {
        l[nod] = it;
        r[it] = nod;
        ++cuplaje;
        return true;
    }
    for (int it : v[nod])
        if (cuplaj(r[it]))
    {
        l[nod] = it;
        r[it] = nod;
        return true;
    }
    return false;
}

int main()
{
    ifstream fin("cuplaj.in");
    fin >> n >> m >> e;
    int x, y;
    for (int i = 0; i < e; i++)
    {
        fin >> x >> y;
        v[x].push_back(y);
    }
    fin.close();
    bool found;
    do
    {
        found = false;
       viz.reset();
       for (int nod = 1; nod <= n; nod++)
            found = (!l[nod]) ? found | cuplaj(nod) : found;
    }
    while (found);
    ofstream fout("cuplaj.out");
    fout << cuplaje << '\n';
    for(int nod = 1; nod <= n; nod++)
            fout << nod << " " << l[nod] << '\n';
    fout.close();
    return 0;
}