Cod sursa(job #3161506)

Utilizator maiaauUngureanu Maia maiaau Data 27 octombrie 2023 12:43:51
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 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;

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

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;
    f >> n >> m >> E;
    while (E--){
        int a, b; f >> 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]});
    g << sol.size() << '\n';
    for (auto it: sol) g << 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;
}