Cod sursa(job #1972893)

Utilizator felixiPuscasu Felix felixi Data 23 aprilie 2017 21:30:13
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

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

const int NMAX = 10000;

int st[NMAX+2], dr[NMAX+2], Ans = 0;
bool viz[NMAX+2];
vector <int> G[NMAX+2];

bool cuplaj( int nod ) {
    if( viz[nod] ) return 0;

    viz[nod] = 1;
    for( auto x : G[nod] ) {
        if( !dr[x] ) {
            st[nod] = x;
            dr[x] = nod;
            ++Ans;
            return 1;
        }
    }
    for( auto x : G[nod] ) {
        if( dr[x] && cuplaj( dr[x] ) ) {
            st[nod] = x;
            dr[x] = nod;
            return 1;
        }
    }
    return 0;
}

int main()
{
    int N, M, E;
    in >> N >> M >> E;
    for( int i = 1;  i <= E;  ++i ) {
        int x,y;
        in >> x >> y;
        G[x].push_back( y );
    }
    bool ok = 1;
    while( ok ) {
        ok = 0;
        memset( viz, 0, sizeof(viz) );
        for( int i = 1;  i <= N;  ++i ) {
            if( !st[i] && cuplaj(i) ) {
                ok = 1;
            }
        }
    }
    out << Ans << '\n';
    for( int i = 1;  i <= N;  ++i ) {
        if( st[i] ) out << i << ' ' << st[i] << '\n';
    }
    return 0;
}