Cod sursa(job #448934)

Utilizator alexandru92alexandru alexandru92 Data 5 mai 2010 06:36:55
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
/* 
 * File:   main.cpp
 * Author: virtualdemon
 *
 * Created on May 5, 2010, 6:23 AM
 */
#include <queue>
#include <cstdlib>
#include <fstream>
#define Nmax 10011
#define oo 0x3f3f3f3f

/*
 * 
 */
using namespace std;
int N;
int d[Nmax], L[Nmax], R[Nmax];
queue< int > Q;
vector< int > G[Nmax];
vector< int >::const_iterator it, iend;
inline bool bfs( void )
{
    int x;
    for( x=1; x <= N; ++x )
        if( !L[x] )
        {
            d[x]=0;
            Q.push(x);
        }
        else d[x]=oo;
    d[0]=oo;
    while( !Q.empty() )
    {
        x=Q.front(); Q.pop();
        if( !x )
            continue;
        for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
            if( oo == d[R[*it]] )
            {
                d[R[*it]]=d[x]+1;
                Q.push(R[*it]);
            }
    }
    return d[0] != oo;
}
inline bool dfs( int x )
{
    if( !x )
        return true;
    vector< int >::const_iterator it=G[x].begin(), iend=G[x].end();
    for( ; it < iend; ++it )
        if( d[R[*it]] == d[x]+1 && dfs(R[*it]) )
        {
            L[x]=*it;
            R[*it]=x;
            return true;
        }
    d[x]=oo;
    return false;
}
int main(int argc, char** argv)
{
    int 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( bfs() )
    {
        for( x=1; x <= N; ++x )
            if( !L[x] && dfs(x) )
                ++nrc;
    }
    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);
}