Cod sursa(job #559254)

Utilizator R.A.RFMI Romila Remus Arthur R.A.R Data 17 martie 2011 18:37:51
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>
#include <vector>
#define nmax 10002

using namespace std;

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

vector<int> V[nmax];
int R[nmax],L[nmax];
bool Uz[nmax];
int N,M,E,cup;

inline bool cupleaza(int nod)
{
    int i;
    if(Uz[nod])return 0;
    Uz[nod] = 1;
    for(i=0;i<V[nod].size();i++)
        if(!R[V[nod][i]]||cupleaza(R[V[nod][i]]))
        {
            L[nod]=V[nod][i],R[V[nod][i]]=nod;
            return 1;
        }
    return 0;
}

int main()
{
    bool cuplez;
    int x,y,i;
    in>>N>>M>>E;
    while(E--)
    {
        in>>x>>y;
        V[x].push_back(y);
    }
    cuplez = 1 ;
    while(cuplez)
    {
        for(i=1;i<=N;i++)
            Uz[i]=0;
        cuplez=0;
        for(i=1;i<=N;i++)
            if(!L[i]&&cupleaza(i))
                cuplez=1,cup++;
    }
    out<<cup<<'\n';
    for(i=1;i<=N;i++)
        if(L[i])out<<i<<' '<<L[i]<<'\n';
    return 0;
}