Cod sursa(job #448719)

Utilizator alexandru92alexandru alexandru92 Data 4 mai 2010 15:51:37
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on May 4, 2010, 2:21 PM
 */
#include <vector>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#define Nmax 10011

/*
 * 
 */
using namespace std;
bool uz[Nmax];
int L[Nmax], R[Nmax];
vector< int > G[Nmax];
inline bool PairUp( int x )
{
    if( uz[x] )
        return false;
    uz[x]=true;
    vector< int >::iterator it=G[x].begin(), iend=G[x].end();
    for( ; it < iend; ++it )
        if( !R[*it] || PairUp( R[*it] ) )
        {
            L[x]=*it;
            R[*it]=x;
            return true;
        }
    return false;
}
int main(int argc, char** argv)
{
    bool was;
    int N, M, E, x, y, nrc=0;
    ifstream in( "cuplaj.in" );
    for( in>>N>>M>>E; E; --E )
    {
        in>>x>>y;
        G[x].push_back(y);
    }
    while( true )
    {
        was=true;
        fill( uz+1, uz+N+1, false );
        for( x=1; x <= N; ++x )
            if( !L[x] && PairUp(x) )
                was=false, ++nrc;
        if( was )
            break;
    }
    ofstream out( "cuplaj.out" );
    out<<nrc<<'\n';
    for( x=1; nrc && x <= N; ++x )
        if( L[x] )
            out<<x<<' '<<L[x]<<'\n', --nrc;
    return (EXIT_SUCCESS);
}