Cod sursa(job #3220177)

Utilizator RobertAcAcatrinei Robert-Marian RobertAc Data 2 aprilie 2024 18:10:38
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <bits/stdc++.h>
#ifdef LOCAL
#define in cin
#define out cout
#endif

using namespace std;

#ifndef LOCAL
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
#endif

const int nmax = 1e4 + 5;

int l[nmax];
int r[nmax];
bool viz[nmax];
vector<int> adj[nmax];

bool pairup(int nod)
{
    if (viz[nod])
        return 0;
    viz[nod] = 1;
    for (auto i : adj[nod])
    {
        if (r[i] == 0)
        {
            r[i] = nod;
            l[nod] = i;
            return 1;
        }
    }
    for (auto i : adj[nod])
    {
        if (pairup(r[i]))
        {
            r[i] = nod;
            l[nod] = i;
            return 1;
        }
    }
    return 0;
}

int main()
{
    int n, m, e;
    in >> n >> m >> e;
    for (int i = 0; i < e; i++)
    {
        int a, b;
        in >> a >> b;
        adj[a].push_back(b);
    }
    int cup = 0;
    bool schimb = 0;
    do
    {
        schimb = 0;
        memset(viz, 0, sizeof viz);
        for (int i = 1; i <= n; i++)
        {
            if (l[i] == 0)
            {
                if (pairup(i))
                {
                    schimb = 1;
                    cup++;
                }
            }
        }
    } while (schimb);
    out << cup << '\n';
    for (int i = 1; i <= n; i++)
    {
        if (l[i] != 0)
        {
            cout << i << ' ' << l[i] << '\n';
        }
    }
}