Cod sursa(job #3319929)

Utilizator alexdraguAlexandru Dragu alexdragu Data 3 noiembrie 2025 20:44:22
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");
vector<int> v[10005];
bool viz[10005],ok;
int match1[10005],match2[10005],n,m,q,x,y,nr,i;
bool match(int nod)
{
    if(viz[nod]) return false;
    viz[nod]=true;
    for(auto x:v[nod])
    {
        if(match2[x]==-1)
        {
            match1[nod]=x;
            match2[x]=nod;
            return true;
        }
    }
    for(auto x:v[nod])
    {
        if(match(match2[x]))
        {
            match1[nod]=x;
            match2[x]=nod;
            return true;
        }
    }
    return false;
}
int main()
{
    cin>>n>>m>>q;
    while(q--)
    {
        cin>>x>>y;
        v[x].push_back(y);
    }
    for(i=1;i<=n;i++) match1[i]=-1;
    for(i=1;i<=m;i++) match2[i]=-1;
    ok=true;
    while(ok)
    {
        ok=false;
        for(i=1;i<=n;i++)
        {
            viz[i]=false;
        }
        for(i=1;i<=n;i++)
        {
            if(match1[i]==-1) ok|=match(i);
        }
    }
    for(i=1;i<=n;i++)
    {
        nr+=(match1[i]!=-1);
    }
    cout<<nr<<'\n';
    for(i=1;i<=n;i++)
    {
        if(match1[i]!=-1)
        {
            cout<<i<<' '<<match1[i]<<'\n';
        }
    }
    return 0;
}