Cod sursa(job #3042002)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 3 aprilie 2023 14:47:49
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <string.h>
#include <vector>

using namespace std;

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

const int N = 1e4;
int l[N + 1], r[N + 1];

bool viz[N + 1];

vector <int> g[N + 1];

int n, m, e, x, y;

bool cuplez (int node)
{
    viz[node] = 1;
    for (auto it : g[node])
        if (!r[it])
        {
            l[node] = it;
            r[it] = node;
            return 1;
        }
    for (auto it : g[node])
        if (!viz[r[it]] && cuplez (r[it]))
        {
            l[node] = it;
            r[it] = node;
            return 1;
        }
    return 0;
}

int main()
{
    for (cin >> n >> m >> e; e-- && cin >> x >> y; )
        g[x].push_back(y);
    int cuplaj = 0;
    while (1)
    {
        memset (viz, 0, sizeof(viz));
        bool ok = false;
        for (int i = 1; i <= n; ++i)
            if (!l[i] && cuplez(i))
                ++cuplaj, ok = true;
        if (!ok)
            break;
    }
    cout << cuplaj << '\n';
    for (int i = 1; i <= n; ++i)
        if (l[i])
        cout << i << ' ' << l[i] << '\n';
    return 0;
}