Cod sursa(job #2392798)

Utilizator Alex_BubBuburuzan Alexandru Alex_Bub Data 30 martie 2019 13:58:21
Problema Cuplaj maxim in graf bipartit Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

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

const int NMax = 10000;

int L[NMax + 5], R[NMax + 5], E, N, M, Ans;

bool Use[NMax + 5];

vector <int> G[NMax + 5];

bool Cuplaj(int Nod)
{
    if(Use[Nod]) return 0;

    Use[Nod] = 1;

    for(auto Vecin : G[Nod])
        if(R[Vecin] == 0)
        {
            L[Nod] = Vecin;
            R[Vecin] = Nod;
            return 1;
        }

    for(auto Vecin : G[Nod])
        if(Cuplaj(R[Vecin]))
        {
            L[Nod] = Vecin;
            R[Vecin] = Nod;
            return 1;
        }
    return 0;
}

void Solve()
{
    bool ok;

    do
    {
        ok = 0; memset(Use, 0, sizeof(Use));

        for(int i = 1; i <= N; i++)
            if(L[i] == 0)
                ok ^= Cuplaj(i);
    }
    while(ok);
}

int main()
{
    fin >> N >> M >> E;

    for(int i = 1, x, y; i <= E; i++)
    {
        fin >> x >> y;

        G[x].push_back(y);
    }
    Solve();

    for(int i = 1; i <= N; i++)
        Ans += (L[i] != 0);

    fout << Ans << '\n';

    for(int i = 1; i <= N; i++)
        if(L[i])
            fout << i << " " << L[i] << '\n';

    fin.close();
    fout.close();

    return 0;
}