Cod sursa(job #2884255)

Utilizator Iulia14iulia slanina Iulia14 Data 2 aprilie 2022 19:06:44
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>

using namespace std;
const int N = 1e4 + 5;
vector <int> g[N];
int loverG[N], loverB[N], viz[N];
int n, m, e;
int bkt(int nod)
{
    if (viz[nod])
        return 0;
    viz[nod] = 1;
    for (auto vecin : g[nod])
    {
        if (!loverG[vecin] || bkt(loverG[vecin]))
        {
            loverG[vecin] = nod;
            loverB[nod] = vecin;
            return 1;
        }
    }
    return 0;
}
int main()
{
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);
    cin >> n >> m >> e;
    for (int i = 1; i <= e; i++)
    {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        //g[b].push_back(a);
    }
    while (true)
    {
        for (int i = 1; i <= n; i++)
            viz[i] = 0;
        int nr = 0;
        for (int i = 1; i <= n; i++)
            if (!loverB[i])
                nr += bkt(i);
        if (!nr)
            break;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
        ans += (loverB[i] != 0);
    cout << ans << '\n';
    for (int i = 1; i <= n; i++)
        if (loverB[i])
            cout << i << " " << loverB[i] << '\n';
    return 0;
}