Cod sursa(job #2987156)

Utilizator maiaauUngureanu Maia maiaau Data 2 martie 2023 00:01:46
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>
using namespace std;

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

#define pb push_back

const int N = 2e4 + 5;

int n, cup[N];
vector<int> v[N];
vector<pair<int, int>> sol;
bitset<N> u;

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

int main(){
    read();
    bool c = true;
    while (c){
        c = false;
        u.reset();
        for (int i = 1; i <= n; i++)
            if (!cup[i] && !u[i])
                if (cupleaza(i))
                    c = true;
    }
    prt();
    
    return 0;
}

void read(){
    int m, e, x, y;
    f >> n >> m >> e;
    for (; e; e--){
        f >> x >> y;
        y += n;
        v[x].pb(y); v[y].pb(x);
    }
}
bool cupleaza(int nod){
    if (u[nod]) return false;
    u[nod] = 1;
    for (auto it: v[nod])
        if (!cup[it]){
            cup[it] = nod;
            cup[nod] = it;
            return true;
        }
    for (auto it: v[nod])
        if (cupleaza(cup[it])){
            cup[it] = nod;
            cup[nod] = it;
            return true;
        }
    return false;
}
void prt(){
    for (int i = 1; i <= n; i++)
        if (cup[i]) 
            sol.pb({i, cup[i] - n});
    g << sol.size() << '\n';
    for (auto it: sol) g << it.first << ' ' << it.second << '\n';
}