Cod sursa(job #3212401)

Utilizator maiaauUngureanu Maia maiaau Data 11 martie 2024 18:11:42
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define pb push_back

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

const int N = 2e4+3;

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

void read(), cupMax();
bool cupleaza(int);

int main()
{
    read();
    cupMax();
    fout << t << '\n';
    for (int i = 1; i <= n; i++)
        if (cup[i])
            fout << i << ' ' << cup[i] - n << '\n';
    return 0;
}

void read(){
    int m, k; fin >> n >> m >> k;
    e.resize(m+n+2); cup.resize(m+n+2);
    while (k--){
        int a, b; fin >> a >> b; b += n;
        e[a].pb(b); e[b].pb(a);
    }
}

bool cupleaza(int nod){
    if (u[nod]) return 0;
    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;
}
void cupMax(){
    bool c = 1;
    do {
        c = 0; u.reset();
        for (int i = 1; i <= n; i++)
            if (!u[i] && !cup[i])
                if (cupleaza(i))
                    t++, c = 1;
    } while (c);
}