Cod sursa(job #2231103)

Utilizator HumikoPostu Alexandru Humiko Data 12 august 2018 23:18:08
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m, e, no_of_Matches;

int f[10005];
int left_Node[10005];
int right_Node[10005];

vector <int> graph[10005];

bool bipartiteMatching (int node)
{
    if ( f[node] )
        return false;

    f[node] = 1;

    for ( auto x:graph[node] )
    {
        if ( left_Node[x] == 0 )
        {
            left_Node[x] = node;
            right_Node[node] = x;

            return true;
        }
    }

    for ( auto x:graph[node] )
    {
        if ( bipartiteMatching(left_Node[x]) )
        {
            left_Node[x] = node;
            right_Node[node] = x;

            return true;
        }
    }

    return false;
}

int main()
{
    fin>>n>>m>>e;
    for ( int i = 1; i <= e; ++i )
    {
        int first_Node, second_Node;
        fin>>first_Node>>second_Node;

        graph[first_Node].push_back(second_Node);
    }

    bool keep_Going = true;
    while ( keep_Going )
    {
        keep_Going = false;

        for ( int i = 0; i <= max(n, m); ++i )
            f[i] = 0;

        for ( int i = 1; i <= n; ++i )
            if ( right_Node[i] == 0 )
                keep_Going |= bipartiteMatching(i);
    }

    for ( int i = 1; i <= n; ++i )
        if ( right_Node[i] )
            no_of_Matches++;

    fout<<no_of_Matches<<'\n';

    for ( int i = 1; i <= n; ++i )
        if ( right_Node[i] )
            fout<<i<<" "<<right_Node[i]<<'\n';
}