Cod sursa(job #2720302)

Utilizator Rares09Rares I Rares09 Data 10 martie 2021 18:33:53
Problema Cuplaj maxim in graf bipartit Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream cin ("cuplaj.in");
ofstream cout ("cuplaj.out");

int n, m, e, nrc;
int cuplat[2][10005], used[2][10005];
vector <int> l[10005], r[10005];

bool cupleaza(int lr, int node, int type)
{
    used[lr][node] = true;

    if(!cuplat[lr][node] && lr == 1)
    {
        used[lr][node] = false;
        return 1;
    }

    if(lr == 0)
    {
        for(auto it = l[node].begin(); it != l[node].end(); ++it)
        {
            if(used[1 - lr][*it] == false && cupleaza(1 - lr, *it, type) == true)
            {
                cuplat[1 - lr][*it] = node;
                cuplat[lr][node] = *it;
                used[lr][node] = false;
                return 1;
            }
        }
    }
    else
    {
        if(cupleaza(1 - lr, cuplat[lr][node], 1 - type) == true)
        {
            used[lr][node] = false;
            return 1;
        }
    }

    used[lr][node] = false;
    return 0;
}

int main()
{
    cin >> n >> m >> e;

    for(int i = 1; i <= e; ++i)
    {
        int a, b;
        cin >> a >> b;
        l[a].push_back(b);
        r[b].push_back(a);
    }

    for(int i = 1; i <= n; ++i)
    {
        if(!cuplat[0][i])
        {
            cupleaza(0, i, 1);

            if(cuplat[0][i])
                ++nrc;
        }
    }

    cout << nrc << '\n';

    for(int i = 1; i <= n; ++i)
    {
        if(cuplat[0][i])
        {
            cout << i << ' ' << cuplat[0][i] << '\n';
        }
    }

    return 0;
}