Cod sursa(job #2784960)

Utilizator guzgandemunteIonescu Laura guzgandemunte Data 17 octombrie 2021 19:10:22
Problema Cuplaj maxim in graf bipartit Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#define NMAX 10000
#define MMAX 10000

using namespace std;

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

vector <int> adj[MMAX];
int pairs[MMAX];
bool used[MMAX];
int N, M, E, x, y;

bool tryKuhn(int u)
{
    if (used[u]) return false;
    used[u] = true;
    for (auto w:adj[u])
        if (pairs[w] == -1 || tryKuhn(pairs[w]))
        {
            pairs[w] = u;
            return true;
        }
    return false;
}

int main()
{
    fin >> N >> M >> E;

    for (int i = 0; i < E; ++i)
    {
        fin >> x >> y;
        adj[x - 1].push_back(y - 1);
    }

    memset(pairs, -1, M*sizeof(int));

    int pairCount = 0;

    for (int i = 0; i < N; ++i)
    {
        memset(used, false, M*sizeof(bool));
        if (tryKuhn(i)) ++pairCount;
    }

    fout << pairCount << endl;

    for (int i = 0; i < M; ++i)
       if (pairs[i] != -1) fout << pairs[i] + 1 << " " << i + 1 << endl;

    fin.close();
    fout.close();
    return 0;
}