Cod sursa(job #3042070)

Utilizator SmauSmau George Robert Smau Data 3 aprilie 2023 21:26:12
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>
#define NMAX 10008

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

int n, m, e, mt[NMAX], nr;
bool viz[NMAX];
vector <int> G[NMAX];

bool DFS(int nod);

int main()
{
    ios_base::sync_with_stdio(0); fin.tie(0);
    int u, v;
    fin >> n >> m >> e;
    for (int i = 1; i <= e; i++)
    {
        fin >> u >> v;
        G[u].push_back(v);
    }

    for (int i = 1; i <= m; i++)
        mt[i] = -1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++) viz[j] = 0;
        DFS(i);
    }

    for (int i = 1; i <= m; i++)
        if (mt[i] != -1)
            nr++;
    fout << nr << '\n';
    for (int i = 1; i <= m; i++)
        if (mt[i] != -1)
            fout << mt[i] << ' ' << i << '\n';
    return 0;
}

bool DFS(int nod)
{
    if (viz[nod] == 1)
        return 0;
    viz[nod] = 1;
    for (auto el : G[nod])
        if (mt[el] == -1 || DFS(mt[el]))
        {
            mt[el] = nod;
            return 1;
        }
    return 0;
}