Cod sursa(job #3162653)

Utilizator marylolloTimbus Maria marylollo Data 29 octombrie 2023 16:44:43
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;

typedef pair<int,int> pii;
#define fi first
#define se second
#define pb push_back

const int N = 2e4+3;


int n, cup[N];
vector<int> e[N];
bitset<N> u;

void read(), cupmax(), prt();
bool cupleaza(int);

int main()
{
    read();
    cupmax();
    prt();

    return 0;
}
void read()
{
    int E, m;
    cin >> n >> m >> E;
    while (E--){
        int a, b; cin >> a >> b; b += n;
        e[a].pb(b); e[b].pb(a);
    }
}
void cupmax(){
    bool c = 1;
    do {
        c = 0;
        u.reset();
        for (int i = 1; i <= n; i++){
            if (!cup[i] && !u[i])
                if (cupleaza(i)) 
                    c = 1;
        }
    } while (c);
}
void prt(){
    vector<pii> sol;
    for (int i = 1; i <= n; i++)
        if (cup[i]) sol.pb({i, cup[i]});
    cout << sol.size() << '\n';
    for (auto it: sol) cout << it.fi << ' ' << it.se - n << '\n';
}
bool cupleaza(int nod){
    if (u[nod]) return false;
    u[nod] = 1;
    for (auto it: e[nod])
        if (!cup[it]){
            cup[it] = nod;
            cup[nod] = it; 
            return 1;
        }
    for (auto it: e[nod])
        if (cupleaza(cup[it])){
            cup[it] = nod;
            cup[nod] = it;
            return 1;
        }
    return 0;
}