Cod sursa(job #2277036)

Utilizator mlupseLupse-Turpan Mircea mlupse Data 5 noiembrie 2018 18:54:58
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int NMax = 10000;
int N,M,E,Sol;
vector <int> G[NMax + 5];
int L[NMax + 5],R[NMax + 5],Use[NMax + 5];
void Read()
{
    fin >> N >> M >> E;
    for(int i = 1; i <= E; ++i)
    {
        int x,y;
        fin >> x >> y;
        G[x].push_back(y);
    }
}

int Pairup(int Nod)
{
    if(Use[Nod] == 1) return 0;
    Use[Nod] = 1;
    for(int i = 0; i < (int)G[Nod].size(); ++i)
    {
        int Vecin = G[Nod][i];
        if(!R[Vecin])
        {
            L[Nod] = Vecin;
            R[Vecin] = Nod;
            return 1;
        }
    }
    for(int i = 0; i < (int)G[Nod].size(); ++i)
    {
        int Vecin = G[Nod][i];
        if(Pairup(R[Vecin]))
        {
            L[Nod] = Vecin;
            R[Vecin] = Nod;
            return 1;
        }
    }
    return 0;
}

void Solve()
{
    for(int i = 1; i <= N ; ++i)
    {
        memset(Use,0,sizeof(Use));
        Pairup(i);
    }
}
void Print()
{
    for(int i = 1; i <= N; ++i)
        Sol += (L[i] != 0);
    fout << Sol << "\n";
    for(int i = 1; i <= N; ++i)
        if(L[i])
            fout << i << " " << L[i] << "\n";
}

int main()
{
    Read();
    Solve();
    Print();
    return 0;
}