Cod sursa(job #2924307)

Utilizator gabriel10tm@gmail.comGabriel Marian [email protected] Data 29 septembrie 2022 08:23:19
Problema Cuplaj maxim in graf bipartit Scor 56
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
#define cin fin
#define cout fout
const int nmx = 5e4+1;
std::vector<int> a[nmx];
int match[nmx];
int viz[nmx];
int n, m, k;
bool Bpm(int k)
{
    for (int to : a[k])
    {
        if(viz[to] == 0)
        {
            viz[to] = 1;
            if (match[to] == -1 || Bpm(match[to]))
            {
                match[to] = k;
                return true;
            }
        }
    }

    return false;
}

int Solve()
{
    int cnt = 0;
    memset(match, -1, sizeof(match));
    for (int i = 1; i <= n; i++)
    {
        memset(viz, 0, sizeof(viz));
        cnt += Bpm(i);
    }
    return cnt;
}

int main()
{
    cin >> n >> m >> k;
    while (k--)
    {
        int b, c;
        cin >> b >> c;
        a[b].push_back(c);
    }
    int cnt = Solve();
    cout << cnt << "\n";
    for (int i=0;i<n;i++)
    {
        if(match[i] != -1)
            cout << match[i] << ' ' << i << '\n';
    }
}