Cod sursa(job #3352276)

Utilizator marap2011Paun Mara marap2011 Data 26 aprilie 2026 11:40:53
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>
using namespace std;

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

const int nmx = 10005 ;

int n1 , n2 , m ;

vector < int > g[nmx] ;
int st[nmx] , dr[nmx] ;
vector < bool > sel ;

int drum ( int node )
{
    sel[node] = 1 ;
    for ( auto it : g[node] )
        if ( dr[it] == 0 )
        {
            st[node] = it ;
            dr[it] = node ;
            return 1 ;
        }
    for ( auto it : g[node] )
        if ( ! sel[dr[it]] )
        {
            if ( drum(dr[it] ) )
            {
                st[node] = it ;
                dr[it] = node ;
                return 1 ;
            }
        }
    return 0 ;
}

int cuplaj ()
{
    int c = 0 ;
    int oldc = -1 ;
    while ( c != oldc )
    {
        oldc = c ;
        for ( int i = 1 ; i <= n1 ; i ++ )
            sel[i] = 0 ;
        for ( int i = 1 ; i <= n1  ; i ++ )
        {
            if ( st[i] == 0 && sel[i] == 0 )
                c += drum(i) ;
        }
    }
    return c ;
}

int main()
{
    fin >> n1 >> n2 >> m ;
    sel.resize(n1+2) ;
    for ( int i = 1 ; i <= m ; i ++ )
    {
        int x , y ;
        fin >> x >> y ;
        g[x].push_back(y) ;
    }
    fout << cuplaj() ;
    fout << '\n' ;
    for ( int i = 1 ; i <= n1 ; i ++ )
    {
        if ( st[i] != 0 )
            fout << i << " " << st[i] << '\n' ;
    }

    return 0;
}