Cod sursa(job #3352566)

Utilizator RaresPoinaruPoinaru-Rares-Aurel RaresPoinaru Data 28 aprilie 2026 20:13:53
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
///Hopcroft Karp
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");
#define cin fin
#define cout fout

const int MAXN=2e4+10;

int n,m,q,l[MAXN],r[MAXN];
vector <int> g[MAXN];
vector <int> a,b;
bool vi[MAXN];

bool match (int node){
    if (vi[node]) return 0;
    vi[node]=1;

    for (auto x:g[node]){
        if (r[x]==0){
            r[x]=node;
            l[node]=x;
            return 1;
        }
    }

    for (auto x:g[node]){
        if (r[x] and match (r[x])){
            l[node]=x;
            r[x]=node;
            return 1;
        }
    }
    return 0;
}

int cuplaj (){
    bool ok=1;
    int rez=0;
    while (ok){
        ok=0;
        for (int i=1;i<=n+m;++i) vi[i]=0;
        for (int i=1;i<=n;++i){
            if (l[i]==0 and match (i)){
                ok=1;
                rez++;
            }
        }
    }
    return rez;
}

signed main()
{
    cin >>n>>m>>q;

    for (int i=1;i<=q;++i){
        int x,y;
        cin >>x>>y;
        y+=n;
        g[x].push_back (y);
        g[y].push_back (x);
    }

    cout <<cuplaj ()<<'\n';

    for (int i=1;i<=n;++i){
        if (l[i]){
            cout <<i<<' '<<l[i]-n<<'\n';
        }
    }
    return 0;
}