Cod sursa(job #2980572)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 16 februarie 2023 17:31:39
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,m,e;
vector<int>g[10005];
int l[10005],r[10005];
bool cuplat[10005];

bool adaug(int nod)
{
    if (cuplat[nod] == true)
        return false;
    cuplat[nod] = true;
    for (int i = 0; i < g[nod].size(); i++)
    {
        int vecin = g[nod][i];
        if (r[vecin] == 0)
        {
            l[nod] = vecin;
            r[vecin] = nod;
            return true;
        }
    }
    for (int i = 0; i < g[nod].size(); i++)
    {
        int vecin = g[nod][i];
        if (adaug(r[vecin]) == true)
        {
            l[nod] = vecin;
            r[vecin] = nod;
            return true;
        }
    }
    return false;
}

int main()
{
    in >> n >> m >> e;
    for (int i = 1; i <= e; i++)
    {
        int x,y;
        in >> x >> y;
        g[x].push_back(y);
    }
    bool not_optimal = true;
    while (not_optimal == true)
    {
        not_optimal = false;
        for (int i = 1; i <= n; i++)
            cuplat[i] = false;
        for (int i = 1; i <= n; i++)
            if (l[i] == 0)
                not_optimal |= adaug(i);
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++)
        if (l[i] != 0)
            cnt++;
    out << cnt << '\n';
    for (int i = 1; i <= n; i++)
        if (l[i] != 0)
            out << i << ' ' << l[i] << '\n';
    return 0;
}