Cod sursa(job #1069811)

Utilizator Athena99Anghel Anca Athena99 Data 30 decembrie 2013 15:52:20
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int nmax= 10000;

vector <int> v[2*nmax+1];
int p[2*nmax+1];
bool u[2*nmax+1];

int pair_up( int x ) {
    if ( u[x]==1 ) {
        return 0;
    }
    u[x]= 1;
    
    for ( vector <int>::iterator it= v[x].begin(); it!=v[x].end(); ++it ) {
        if ( p[*it]==0 || pair_up(p[*it])==1 ) {
            p[x]= *it;
            p[*it]= x;
            return 1;
        }
    }

    return 0;
}

int main(  ) {
    int n, m, e;
    fin>>n>>m>>e;
    for ( ; e>0; --e ) {
        int x, y;
        fin>>x>>y;
        v[x].push_back(n+y);
        v[n+y].push_back(x);
    }

    int f= 0, x= 1;
    while ( x>0 ) {
        x= 0;
        for ( int i= 1; i<=n; ++i ) {
            u[i]= 0;
        }
        for ( int i= 1; i<=n; ++i ) {
            if ( p[i]==0 ) {
                x+= pair_up(i);
            }
        }
        f+= x;
    }

    fout<<f<<"\n";
    for ( int i= 1; i<=n; ++i ) {
        if ( p[i]>0 ) {
            fout<<i<<" "<<p[i]-n<<"\n";
        }
    }

    return 0;
}