Cod sursa(job #3125733)

Utilizator usureluflorianUsurelu Florian-Robert usureluflorian Data 4 mai 2023 11:42:05
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 1e4 + 3;

int st[nmax], dr[nmax], a, b, n, t1, t2, ok, sol;
bool viz[nmax];

vector <int> v[nmax];

bool cuplaj(int nod)
{
    if (viz[nod])
        return 0;

    viz[nod] = 1;

    for (int i = 0; i < v[nod].size(); ++i)
    {
        int urm = v[nod][i];

        if (!dr[urm])
        {
            dr[urm] = nod;
            st[nod] = urm;
            return 1;
        }
    }

    for (int i = 0; i < v[nod].size(); ++i)
    {
        int urm = v[nod][i];
        if (cuplaj(dr[urm]))
        {
            dr[urm] = nod;
            st[nod] = urm;
            return 1;
        }
    }

    return 0;
}

int main()
{
    f >> a >> b >> n;

    for (int i = 1; i <= n; ++i)
    {
        f >> t1 >> t2;
        v[t1].push_back(t2);
    }

    ok = 1;

    while (ok)
    {
        ok = 0;
        memset(viz, 0, sizeof(viz));

        for (int i = 1; i <= a; ++i)
        {
            if (!st[i])
                if (cuplaj(i))
                    ok = 1;
        }
    }

    for (int i = 1; i <= a; ++i)
        sol += (st[i] != 0);

    g << sol << '\n';

    for (int i = 1; i <= a; ++i)
        if (st[i])
            g << i << ' ' << st[i] << '\n';

    return 0;
}