Cod sursa(job #2882367)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 31 martie 2022 13:03:09
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <bits/stdc++.h>
#define DIM 10005

using namespace std;

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

int n, m, e, L[DIM], R[DIM];
bitset <DIM> viz;
vector <int> edges[DIM];

bool cuplaj(int node)
{
    if (viz[node])
        return 0;
    viz[node] = 1;

    // cupleaza cu nod necuplat
    for (auto k: edges[node])
        if (!R[k])
        {
            R[k] = node;
            L[node] = k; // 38
            return 1;
        }

    // cupleaza cu un nod deja cuplat
    for (auto k: edges[node])
        if (cuplaj(R[k]))
        {
            R[k] = node;
            L[node] = k;
            return 1;
        }
    return 0;
}

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

    for (int i = 1; i <= e; i++)
    {
        int x, y;
        f >> x >> y;
        edges[x].push_back(y);
    }

    bool ok = 0;
    do {
        ok = 0;
        viz.reset();
        for (int i = 1; i <= n; i++)
            if (L[i] == 0)
                ok |= cuplaj(i);
    } while (ok == 1);

    int ans = 0;
    for (int i = 1; i <= n; i++)
        if (L[i])
            ans++;
    g << ans << "\n";
    for (int i = 1; i <= n; i++)
        if (L[i])
            g << i << " " << L[i] << "\n";

    return 0;
}