Cod sursa(job #2137039)

Utilizator andreigeorge08Sandu Ciorba andreigeorge08 Data 20 februarie 2018 15:44:13
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>

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

int viz[10005], dr[10005], st[10005], n, m, e;

vector <int> L[10005];

void Citire()
{
    int x, y;

    fin >> n >> m >> e;

    while(e--)
    {
        fin >> x >> y;
        L[x].push_back(y);
    }
}

int Cupleaza(int x)
{
    if(viz[x]) return 0;

    viz[x] = 1;

    for(auto i : L[x])
        if(dr[i] == 0)
        {
            st[x] = i;
            dr[i] = x;
            return 1;
        }

    for(auto i : L[x])
        if(Cupleaza(dr[i]))
        {
            st[x] = i;
            dr[i] = x;
            return 1;
        }

    return 0;
}

void Rezolva()
{
    int gata = 0, cuplaje = 0;

    while(gata == 0)
    {
        gata = 1;
        for(int i = 1; i <= n; i++) viz[i] = 0;

        for(int i = 1; i <= n; i++)
            if(st[i] == 0 && Cupleaza(i))
                cuplaje++,
                gata = 0;
    }
    fout << cuplaje << "\n";

    for(int i = 1; i <= n; i++)
        if(st[i] != 0)
            fout << i << " " << st[i] << "\n";
}
int main()
{
    Citire();
    Rezolva();
    return 0;
}