Cod sursa(job #2960341)

Utilizator valentin12Valentin Ion Semen valentin12 Data 4 ianuarie 2023 02:10:13
Problema Cuplaj maxim in graf bipartit Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>

using namespace std;

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

int N, M, E, u, v, match[10001];
bool viz[10001];
vector<vector<bool> > G;

int pairup(int n)
{

if (viz[n])
        return 0;
    viz[n] = true;

    for (int i = 1; i <= M; i++)
        if (G[n][i] && (match[i] == 0 || pairup(match[i])))
        {
            match[i] = n;
            return 1;
        }

    return 0;

}

int main()
{

    f >> N >> M >> E;

    G.resize(N + 1);

    for(int i = 0; i <= N; i++)
        G[i].resize(M + 1);

    for(int i = 1; i <= E; i++)
    {
        f >> u >> v;
        G[u][v] = 1;
    }

    int sol = 0;

    for (int i = 1; i <= N; i++)
    {
        memset(viz, 0, sizeof(viz));
        sol += pairup(i);
    }

    g << sol << '\n';

    for (int i = 1; i <= M; i++)
        if (match[i])
            g << match[i] << " " << i << '\n';



    return 0;

}