Cod sursa(job #862206)

Utilizator SteveStefan Eniceicu Steve Data 22 ianuarie 2013 13:46:10
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

int N, M, E;
vector <int> muchii[10011];
int viz[10011];
int Right_match[10011];
int Left_match[10011];

void Citire ()
{
    ifstream fin ("cuplaj.in");
    fin >> N >> M >> E;
    for (int i = 0, a, b; i < E; i++)
    {
        fin >> a >> b;
        muchii[a].push_back (b);
    }
    fin.close ();
}

int DFS (int node)
{
    if (viz[node]) return 0;
    viz[node] = 1;
    for (int i = muchii[node].size () - 1; i >= 0; i--)
    {
        if (!Right_match[muchii[node][i]] || DFS (Right_match[muchii[node][i]]))
        {
            Left_match[node] = muchii[node][i];
            Right_match[muchii[node][i]] = node;
            return 1;
        }
    }
    return 0;
}

int main ()
{
    Citire ();
    int nr_cuplaj = 0;
    for (int i = 1; i <= N; i++)
    {
        if (!Left_match[i])
        {
            if (!DFS (i))
            {
                memset (viz, 0, sizeof (viz));
                nr_cuplaj += DFS (i);
            }
            else nr_cuplaj++;
        }
    }
    ofstream fout ("cuplaj.out");
    fout << nr_cuplaj << "\n";
    for (int i = 1; i <= N; i++)
    {
        if (Left_match[i])
        {
            fout << i << " " << Left_match[i] << "\n";
        }
    }
    fout.close ();
    return 0;
}