Cod sursa(job #2913681)

Utilizator tiut_cristianTiut Cristian tiut_cristian Data 15 iulie 2022 23:44:25
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, V)    for (vector <int>::iterator i = (V).begin(); i != (V).end(); ++ i)

vector <int> G[10005];

int l[10005], r[10005], u[10005];

int pairup(const int n)
{
    if (u[n])
        return 0;

    u[n] = 1;
    FOR (i, G[n])
        if (!r[*i])
        {
            l[n] = *i;
            r[*i] = n;
            return 1;
        }
    FOR (i, G[n])
        if (pairup(r[*i]))
        {
            l[n] = *i;
            r[*i] = n;
            return 1;
        }

    return 0;
}

int main(void)
{
    ifstream fin("cuplaj.in");
    ofstream fout("cuplaj.out");

    int n, m, cnt_edges;

    fin >> n >> m >> cnt_edges;
    for (int i = 1; i <= cnt_edges; i++)
    {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
    }

    int change = 1;
    while (change)
    {
        change = 0;
        memset(u, 0, sizeof(u));
        for(int i = 1; i <= n; i++)
            if (!l[i])
                change |= pairup(i);
    }

    int cuplaj = 0;
    for(int i = 1; i <= n; i++)
        cuplaj += (l[i] > 0);
    fout << cuplaj << '\n';

    for(int i = 1; i <= n; i++)
        if (l[i] > 0)
            fout << i << ' ' << l[i] << '\n';

    return 0;
}