Cod sursa(job #3040597)

Utilizator CobzaruAntonioCobzaru Paul-Antonio CobzaruAntonio Data 30 martie 2023 10:08:08
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream cin ("cuplaj.in");
ofstream cout ("cuplaj.out");
bool viz[100005];
bool cuplaj[10005];
int p[10005];
vector<int> g[10005];
int n,m,e;
bool dfs (int k)
{
    viz[k] = true;
    for (auto vf:g[k])
    {
        if (p[vf]==0 || (viz[p[vf]]==false && dfs(p[vf])==true))
        {
            p[vf] = k;
            cuplaj[k] = true;
            return true;
        }
    }
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> e;
    int i;
    for(i=1;i<=e;i++)
    {
        int x,y;
        cin >> x >> y;
        g[x].push_back(y);
    }
    bool gas = true;
    int rasp = 0;
    while (gas)
    {
        gas = false;
        for (i=1;i<=n;i++)
            viz[i] = false;
        for(i=1;i<=n;i++)
            if (cuplaj[i]==false && dfs(i)==true)
            {
                rasp++;
                gas = true;
            }
    }
    cout << rasp << '\n';
    for(i=1;i<=m;i++)
        if (p[i])
            cout << p[i] << ' ' << i << '\n';
    return 0;
}