Cod sursa(job #3154679)

Utilizator NathanBBerintan Emanuel Natanael NathanB Data 5 octombrie 2023 16:49:50
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
#define cin fin
#define cout fout
#define pb push_back
#define eb emplace_back
#define Inf 0x3f3f3f3f
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const int Nmax = 10005;

int n,m,k,l[Nmax],r[Nmax];
bool vis[Nmax];
vl G[Nmax];

bool findmatch(int nod)
{
    if(vis[nod])
        return false;
    vis[nod] = true;
    for(auto c:G[nod])
        if(!r[c])
    {
        l[nod] = c;
        r[c] = nod;
        return true;
    }

    for(auto c:G[nod])
        if(findmatch(r[c]))
    {
        l[nod] = c;
        r[c] = nod;
        return true;
    }
    return false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>k;
    for(int i=1;i<=k;i++)
    {
        int x,y;
        cin>>x>>y;
        G[x].eb(y);
    }
    int cnt = 0;
    bool match = true;
    while(match)
    {
        memset(vis,false,sizeof vis);
        match = false;
        for(int i=1;i<=n;i++)
            if(!l[i])
                match|=findmatch(i);
    }

    for(int i=1;i<=n;i++)
        if(l[i] > 0)
        cnt++;
    cout<<cnt<<'\n';
    for(int i=1;i<=n;i++)
        if(l[i]>0)
        cout<<i<<" "<<l[i]<<'\n';

    return 0;
}