Cod sursa(job #2836199)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 19 ianuarie 2022 21:49:36
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

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

int n1, n2, m, ans;
int l[10005], r[10005];
bool used[10005];
vector <int> G[10005];

bool pairup(int x)
{
    if(used[x])
        return 0;
    used[x] = 1;
    for(int vecin : G[x])
    {
        if(!r[vecin])
        {
            l[x] = vecin;
            r[vecin] = x;
            return 1;
        }
    }
    for(int vecin : G[x])
    {
        if(pairup(r[vecin]))
        {
            l[x] = vecin;
            r[vecin] = x;
            return 1;
        }
    }
    return 0;
}

int main()
{
    fin >> n1 >> n2 >> m;
    for(int i = 1; i <= m; i++)
    {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
    }
    bool changed = 1;
    while(changed)
    {
        memset(used, 0, sizeof used);
        changed = 0;
        for(int i = 1; i <= n1; i++)
            if(!l[i])
                changed |= pairup(i);
    }
    for(int i = 1; i <= n1; i++)
        if(l[i])
            ans++;
    fout << ans << '\n';
    for(int i = 1; i <= n1; i++)
        if(l[i])
        fout << i << ' ' << l[i] << '\n';
    return 0;
}