Cod sursa(job #755986)

Utilizator Theorytheo .c Theory Data 8 iunie 2012 15:12:48
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include<fstream>
#include<vector>
#include<string.h>
#define nmax 10010

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

vector <int> G[nmax];
int L[nmax], R[nmax], N, M, E;
bool used[nmax];
 int Nr = 0 ;

bool pairup(int x)
{
    if(used[x] == true) return false;
    used[x] = true;
    for(int i = 0 ; i < G[x].size(); ++i )
        {
            int nod = G[x][i];
            if(R[nod] == 0 || pairup(R[nod]))
            {
               L[x] = nod;
               R[nod] = x;
               return true;
            }
        }
    return false;
}

void cuplaj()
{
    bool ok = true;

    while(ok)
    {
        memset(used, 0, sizeof(bool) * (N + 1));
        ok = false;
        for(int i = 1; i <= N; i++)
          //  if( L[i] == 0 )
                    if( L[i] == 0  && pairup(i) )
                {
                    //pairup(i);
                    ++Nr;
                    ok = true;
                }
    }
}

void read()
{
    fin >> N >> M>> E;
    while(E)
    {
        int x, y ;
        fin>> x>> y;
        G[x].push_back(y);
        --E;
    }
    cuplaj();
}
int main()
{
    read();
    fout << Nr <<'\n';
    for(int i = 1; i <= N; i++)
        if(L[i])
            fout << i << " " <<L[i] <<'\n';
    fin.close();
    return 0;

}