Cod sursa(job #1550441)

Utilizator bogdanRUSURusu Bogdan bogdanRUSU Data 13 decembrie 2015 18:57:02
Problema Cuplaj maxim in graf bipartit Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 2.61 kb
#include <fstream>
#include <vector>

using namespace std;
ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");
unsigned N1, N2, M;
vector < vector <int> > graph;
vector <bool> viz;
vector <int> C;

void read()
{
    int x, y;
    fin >> N1 >> N2 >> M;
    graph.resize( N1 + N2 + 1 );
    for ( unsigned i = 1; i <= M; ++i )
    {
        fin >> x >> y;
        graph[x].push_back( y );
        graph[ N1 + y ].push_back( x );
    }
}

void afisare ()
{
    unsigned i;
    for ( i = 1; i <= N1; ++i )
    {
        if ( C[i] != -1 )
        {
            if ( C[i] > N1 )
            {
                fout << i << " " << C[i] - N1 << '\n';
                continue;
            }
            fout << i << " " << C[i] << '\n';
        }
    }
}

void match ( int x, bool nrGraph )
{
    viz[x] = true;
    unsigned i;
    for ( i = 0; i < graph[x].size(); ++i)
    {
        if ( !viz[ nrGraph * N1 + graph[x][i] ] )
        {
            if ( C[x] == -1 && C[nrGraph * N1 + graph[x][i]] == -1 )
            {
                C[x] = nrGraph * N1 + graph[x][i];
                C[nrGraph * N1 + graph[x][i]] = x;
                match ( graph[x][i] + nrGraph * N1, !nrGraph );
            }
            else
                match ( graph[x][i] + nrGraph * N1, !nrGraph );
        }
    }
}

void init ( bool Init )
{
    unsigned i;
    for ( i = 0; i <= viz.size(); ++i )
        viz[i] = Init;
}
int dfs ( int x )
{
    viz[x] = true;
    unsigned j;
    for ( j = 0; j < graph[x].size(); ++j )
    {
        int k = graph[x][j] + N1;
        if ( viz[k] || C[x] == k )
        {
            viz[k] = true;
            continue;
        }
        if ( C[k] == -1 )
        {
            viz[k] = true;
            C[k] = x;
            C[x] = k;
            return 1;
        }
        else
        {
            viz[k] = 1;
            if ( dfs( C[k] ) )
            {
                C[x] = k;
                C[k] = x;
                return 1;
            }
            else
                continue;
        }
    }
    return 0;
}
int main()
{
    read();
    viz.resize( N1 + N2 + 1, false );
    C.resize( N1 + N2 + 1, -1 );
    match ( 1, true );
    unsigned i, ans = 0;
    bool ok = true;
    while ( ok )
    {
        ok = false;
        init( false );
        for ( i = 1; i <= N1; ++i )
            if ( viz[i] || C[i] == -1 )
                if ( dfs( i ) )
                    ok = true;
    }
    for ( i = 1; i <= N1; ++i )
        if ( C[i] > -1 )
            ++ans;
    fout << ans << '\n';
    afisare();
    return 0;
}