Cod sursa(job #3347683)

Utilizator WiseAndrei4Vetrila Andrei WiseAndrei4 Data 17 martie 2026 20:36:58
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#define ll long long
using namespace std;
ifstream fin("cuplaj.in");
ofstream gout("cuplaj.out");
vector<vector<int>>g(10001);
int n,m,e,l[10001],r[10001],maxMatch;
bool viz[10001];

bool match(int x)
{
    if(viz[x])return 0;
    viz[x]=1;
    for(const auto &y:g[x])if(!r[y])
        {
            l[x]=y;
            r[y]=x;
            return 1;
        }
    for(const auto &y:g[x])if(match(r[y]))
        {
            l[x]=y;
            r[y]=x;
            return 1;
        }
    return 0;
}
void hk()
{
    bool changed=1;
    int i;
    while(changed)
    {
        changed=0;
        for(i=1; i<=n; ++i)viz[i]=0;
        for(i=1; i<=n; ++i)if(!l[i]&&match(i))
            {
                changed=1;
                ++maxMatch;
            }
    }
}
int main()
{
    int x,y;
    fin>>n>>m>>e;
    while(e--)
    {
        fin>>x>>y;
        g[x].push_back(y);
    }
    hk();
    gout<<maxMatch<<'\n';
    for(int i=1; i<=n; ++i)if(l[i])gout<<i<<' '<<l[i]<<'\n';
    return 0;
}