Cod sursa(job #1662176)

Utilizator ArambasaVlad Arambasa Arambasa Data 24 martie 2016 15:47:32
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
const int Nmax = 10005;
vector <int> G[Nmax];
int l, r, m, Left[Nmax], Right[Nmax], Use[Nmax], nr;
void Read()
{
    in>>l>>r>>m;
    for(int i = 1; i <= m; i++)
    {
        int x,y;
        in>>x>>y;
        G[x].push_back(y);
    }
}
int pairup(int nod)
{
    if(Use[nod]) return 0;
    Use[nod] = 1;
    for(int i = 0; i < (int)G[nod].size(); i++)
    {
        int vecin = G[nod][i];
        if(!Right[vecin])
        {
            Left[nod] = vecin;
            Right[vecin] = nod;
            return 1;
        }
    }
    for(int i = 0; i < (int)G[nod].size(); i++)
    {
        int vecin = G[nod][i];
        if(pairup(Right[vecin]))
        {
            Left[nod] = vecin;
            Right[vecin] = nod;
            return 1;
        }
    }
    return 0;
}
void Solve()
{
    int ok = 0;

    do
    {
        ok = 0;
        memset(Use, 0, sizeof(Use));
        for(int i = 1; i <= l; i++)
        {
            if(Left[i] == 0)
            {
                if(pairup(i))
                {
                    ok = 1;
                    nr++;
                }
            }
        }
    }while(ok == 1);
}
void Print()
{
    out<<nr<<'\n';
    for(int i = 1; i <= l; i++)
    {
        if(Left[i] > 0) out<<i<<' '<<Left[i]<<'\n';
    }
}
int main()
{
    Read();
    Solve();
    Print();
    return 0;
}