Mai intai trebuie sa te autentifici.

Cod sursa(job #1261903)

Utilizator xtreme77Patrick Sava xtreme77 Data 12 noiembrie 2014 20:16:24
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.64 kb
/*
 * Code by Spiromanul
 */

#include <fstream>
#include <bitset>
#include <vector>

#define pb push_back

const char IN [ ] = "cuplaj.in" ;
const char OUT [ ] = "cuplaj.out" ;
const int MAX = 10014 ;

using namespace std;

ifstream fin ( IN ) ;
ofstream fout ( OUT ) ;

vector < int > gr [ MAX ] ;

bitset < MAX > viz ;

int rightboss [ MAX ] , leftboss [ MAX ] ;

bool cuplaj ( int nod )
{
    if ( viz [ nod ] )
        return 0 ;
    viz [ nod ] = 1 ;
    for ( auto x : gr [ nod ] )
    {
        if ( rightboss [ x ] == 0 )
        {
            leftboss [ nod ] = x ;
            rightboss [ x ] = nod ;
            return 1 ;
        }
    }
    for ( auto x : gr [ nod ] )
        if ( cuplaj ( rightboss [ x ] ) )
        {
            leftboss [ nod ] = x ;
            rightboss [ x ] = nod ;
            return 1 ;
        }
    return 0 ;
}

int main (              )
{
    int st , dr , n ;
    fin >> st >> dr >> n ;
    for ( int i = 1 ; i <= n ; ++ i )
    {
        int x , y ;
        fin >> x >> y ;
        gr [ x ].pb ( y ) ;
    }
    int ok = 1 ;
    do {
        ok = 0 ;
        viz.reset ( ) ;
        for ( int i = 1 ; i <= st ; ++ i )
            if ( leftboss [ i ] == 0 )
                if ( cuplaj ( i ) )
                    ok = 1 ;
    }while ( ok ) ;
    /////////////
    int ce_trebuie = 0 ;
    for ( int i = 1 ; i <= st ; ++ i )
        if ( leftboss [ i ] )
            ++ ce_trebuie ;
    fout << ce_trebuie << '\n' ;
    for ( int i = 1 ; i <= st ; ++ i )
        if ( leftboss [ i ] )
            fout << i << ' ' << leftboss [ i ] << '\n' ;
    return 0;
}