Cod sursa(job #2663969)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 27 octombrie 2020 18:18:57
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
#include <vector>
#include <bitset>
#define f in
#define g out

using namespace std;
ifstream in ( "cuplaj.in" );
ofstream out( "cuplaj.out" );
int n, m, k, i, j, ok, x, y, sol;
int st[10100], dr[10100];
vector<int> L[10100];
bitset<10100> fr;

int cupleaza( int nod ){
    if ( fr[nod] )
        return 0;
    fr[nod] = 1;
    
    for ( auto vecin: L[nod] )
        if ( !dr[vecin] ){
            st[nod] = vecin;
            dr[vecin] = nod;
            sol++;
            return 1;
        }
    
    for ( auto vecin: L[nod] )
        if( cupleaza(dr[vecin]) ){
            st[nod] = vecin;
            dr[vecin] = nod;
            return 1;
        }
    return 0;
}


int main() {
    f>>n>>m>>k;
    for ( i=1; i <= k; i++ ){
        f>>x>>y;
        L[x].push_back(y);
    }
    
    ok = 1;
    while ( ok ) {
        ok = 0;
        fr.reset();
        for ( i=1; i <= n; i++ )
            if ( st[i] == 0 && cupleaza(i) )
                ok = 1;
    }
    g<<sol<<"\n";
    for ( i=1; i <= n; i++ )
        if ( st[i] )
            g<<i<<" "<<st[i]<<"\n";
    return 0;
}