Cod sursa(job #3030068)

Utilizator MihaiCostacheCostache Mihai MihaiCostache Data 17 martie 2023 14:51:09
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m, e, x, y, viz[10001], st[10001], dr[10001], muchii;
vector <int> l[10001];

int cupleaza(int nod)
{
    if(viz[nod]!=0)
        return false;
    viz[nod]=1;
    for(int i=0; i<l[nod].size(); i++)
    {
        int vecin=l[nod][i];
        if(dr[vecin]==0)
        {
            muchii++;
            dr[vecin]=nod;
            st[nod]=vecin;
            return 1;
        }
    }
    for(int i=0; i<l[nod].size(); i++)
    {
        int vecin=l[nod][i];
        if(cupleaza(dr[vecin]))
        {
            dr[vecin]=nod;
            st[nod]=vecin;
            return 1;
        }
    }
    return 0;
}

int main()
{
    fin>>n>>m>>e;
    for(int i=1; i<=e; i++)
    {
        fin>>x>>y;
        l[x].push_back(y);
    }
    int ok=0;
    do
    {
        ok=1;
        memset(viz, 0, sizeof(viz));
        for(int i=1; i<=n; i++)
        {
            if(st[i]==0 && cupleaza(i))
                ok=0;
        }
    }while(!ok);
    fout<<muchii<<"\n";
    for(int i=1; i<=n; i++)
    {
        if(st[i]!=0)
        {
            fout<<i<<" "<<st[i]<<"\n";
        }
    }
    return 0;
}