Cod sursa(job #2695904)

Utilizator TraianVVisan Traian-Dimitrie TraianV Data 14 ianuarie 2021 20:16:58
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>
#define pb push_back
#define Nmax 10005
#define Mmax 10005
using namespace std;

vector < int > V[10005];

int N, M, E, x, y, Cmax, L[Nmax], R[Mmax];
bool Marc[Nmax], ok;

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

int cuplez (int x)
{
    Marc[x] = true;

    for(auto y: V[x])
        if(!R[y])
        {
            L[x] = y;
            R[y] = x;

            return 1;
        }

    for(auto y: V[x])
        if(!Marc[R[y]] && cuplez(R[y]))
        {
            L[x] = y;
            R[y] = x;

            return 1;
        }

    return 0;

}
void cuplaj()
{
    bool ok =  true;

    while (ok)
    {
        ok = false;
        for(int i = 1; i <= N; i++)
            Marc[i] = false;

        for(int i = 1; i <= N; i++)
            if(!L[i] && !Marc[i] && cuplez(i))
                {
                    ok = true;
                    Cmax++;
                }
    }
}
int main()
{
    f >> N >> M >> E;

    for ( ; E; E--)
    {
        f >> x >> y;
        V[x].pb(y);
    }

    cuplaj();

    g << Cmax << '\n';

    for (int i = 1; i <= N; ++i)
        if(L[i]) g << i << " " << L[i] << '\n';
    f.close();
    g.close();
    return 0;
}