Cod sursa(job #1496894)

Utilizator CiurezAndreiCiurez Marius-Andrei CiurezAndrei Data 5 octombrie 2015 18:55:39
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <fstream>
#include <vector>

#define DIM 100001

using namespace std;

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

int stanga[DIM], dreapta[DIM];
bool v[DIM], ok;

vector<int>l[DIM];

int n, m, e, a, b, nr;

bool cuplaj(int nod)
{
    v[nod] = true;

    for(int i = 0; i < l[nod].size(); i ++)
    {
        int a = l[nod][i];

        if(dreapta[a] == 0)
        {
            dreapta[a] = nod;
            stanga[nod] = a;
            return true;
        }
    }

    for(int i = 0; i < l[nod].size(); i ++)
    {
        int a = l[nod][i];

        if(v[dreapta[a]] == false && cuplaj(dreapta[a]) == true )
        {
            dreapta[a] = nod;
            stanga[nod] = a;
            return true;
        }
    }

    return false;
}

int main()
{
    fin >> n >> m >> e;

    for(int i = 1; i <= e; i ++)
    {
        fin >> a >> b;
        l[a].push_back(b);
    }

    while(1)
    {
        ok = false;

        for(int i = 1; i <= n; i ++)
            v[i] = false;

        for(int i = 1; i <= n;  i ++)
        {
            if(stanga[i] == 0 && cuplaj(i) == true)
            {
                ok =  true;
                nr ++;
            }
        }

        if(ok == false)
        {
            break;
        }

    }

    fout << nr << '\n';

    for(int i = 1; i <= n; i ++)
    {
        if(stanga[i] != 0)
        {
            fout << i << " " << stanga[i] << '\n';
        }
    }

    return 0;
}