Cod sursa(job #3032637)

Utilizator stefanvoicaVoica Stefan stefanvoica Data 22 martie 2023 15:51:52
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>
#define int long long
//#define mod 1000000007
using namespace std;
ifstream fin ("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m, sol, st[10002], dr[10002];
bool viz[10002];
vector <int> a[10002];

bool cupleaza (int x)
{
    if (viz[x])
        return 0;
    viz[x]=1;
    for (auto y:a[x])
    if (dr[y]==0)
    {
        st[x]=y;
        dr[y]=x;
        sol++;
        return 1;
    }
    for (auto y:a[x])
    if (cupleaza(dr[y]))
    {
        st[x]=y;
        dr[y]=x;
        return 1;
    }
    return 0;
}

void cuplaj()
{
    bool cuplat=1;
    while (cuplat)
    {
        for (int i=1;i<=n;i++)
            viz[i]=0;
        cuplat=0;
        for (int i=1;i<=n;i++)
        if (st[i]==0)
        {
            if (cupleaza(i))
                cuplat=1;
        }
    }
}

int32_t main()
{
    int nrm,x,y;
    fin>>n>>m>>nrm;
    while (nrm--)
    {
        fin>>x>>y;
        a[x].push_back(y);
    }
    cuplaj();
    fout<<sol<<'\n';
    for (int i=1;i<=n;i++)
        if (st[i]!=0)
        fout<<i<<' '<<st[i]<<'\n';
    return 0;
}