Cod sursa(job #2376256)

Utilizator liviu2000Dragomirescu Liviu liviu2000 Data 8 martie 2019 14:30:32
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>
#define N 10005

using namespace std;

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

int a , b , e ,sol;
int lft[N] ,rght[N] ;
bool viz[N] ;
vector<int> graf[N] ;

void citire()
{
    int i , x , y ;
    fin >> a >> b >> e ;
    for ( i = 1 ; i <= e ; i++ )
    {
        fin >> x >> y ;
        graf[x].push_back(y) ;
        graf[y].push_back(x) ;

    }
}

bool cuplaj(int nod)
{
    if ( viz[nod] == true )
        return false;
    viz[nod] = false ;
    for ( auto vec : graf[nod] )
    {
        if ( rght[vec] == 0 )
        {
            lft[vec] = nod ;
            rght[nod] = vec ;
            sol++ ;
            return true ;
        }
    }
    for ( auto vec : graf[nod] )
    {
        if ( cuplaj(rght[vec]) == true )
        {
            lft[vec] = nod ;
            rght[nod] = vec ;
            return true ;
        }
    }
    return false;
}

int main()
{
    int i ;
    bool ok = false;
    citire() ;
    do
    {
        ok = false;
        for ( i = 1 ; i <= a ; i++ )
            viz[i] = true ;
        for ( i = 1 ; i <= a ; i++ )
            if ( lft[i] == 0 )
                ok = ok|cuplaj(i) ;
    } while ( ok == true ) ;
    fout << sol << '\n' ;
    for ( i = 1 ; i <= a ; i++ )
        if ( lft[i] )
            fout << i << " " << lft[i] << '\n' ;
}