Cod sursa(job #675027)

Utilizator R.A.RFMI Romila Remus Arthur R.A.R Data 6 februarie 2012 23:35:45
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#include <vector>
#define nmax 10003

using namespace std;

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

vector<int> V[nmax];
int Right[nmax],Left[nmax];
bool Uz[nmax];
int N,M,E,cuplu;

inline bool cupleaza(int nod)
{
    if(Uz[nod])return 0;//e cuplat deja
    Uz[nod] = 1;
    int y,i;
    for(i=0;i<V[nod].size();++i)
    {
        y = V[nod][i];
        if(!Right[y]||cupleaza(Right[y]))//ori y nu e cuplat ori ii cuplez perechea iar pe el in pun cu nod
        {
            Left[nod]=y;
            Right[y]=nod;
            return 1;
        }
    }
    return 0;
}

int main()
{
    int i,x,y;
    bool cuplez;
    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(!Left[i]&&cupleaza(i))//nu e cuplat
                cuplez = 1,cuplu++;

    }
    out<<cuplu<<'\n';
    for(i=1;i<=N;i++)
        if(Left[i])
            out<<i<<' '<<Left[i]<<'\n';
    in.close();
    out.close();
    return 0;
}