Cod sursa(job #3041751)

Utilizator Tudor06MusatTudor Tudor06 Data 1 aprilie 2023 13:47:41
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;

const int NMAX = 1e4;

vector <int> edges[2 * NMAX + 1];
bool viz[2 * NMAX + 1];
int pereche[2 * NMAX + 1];

bool reimperechere( int node ) {
    viz[node] = true;
    for ( auto vec : edges[node] ) {
        if ( !pereche[vec] || ( !viz[pereche[vec]] && reimperechere( pereche[vec] ) ) ) {
            pereche[node] = vec;
            pereche[vec] = node;
            return true;
        }
    }
    return false;
}
int main() {
    ifstream fin( "cuplaj.in" );
    ofstream fout( "cuplaj.out" );
    int n, m, e;
    fin >> n >> m >> e;
    for ( int i = 0, a, b; i < e; i ++ ) {
        fin >> a >> b;
        edges[a].push_back( b + n );
        edges[b + n].push_back( a );
    }
    int cuplaj = 0;
    bool sepoate = true;
    while ( sepoate ) {
        sepoate = false;
        for ( int i = 1; i <= n; i ++ ) viz[i] = false;
        for ( int i = 1; i <= n; i ++ ) {
            if ( !viz[i] && !pereche[i] && reimperechere( i ) ) {
                sepoate = true;
                cuplaj ++;
            }
        }
    }
    fout << cuplaj << '\n';
    for ( int i = 1; i <= n; i ++ ) if ( pereche[i] ) fout << i << ' ' << pereche[i] - n << '\n';
    return 0;
}