Cod sursa(job #3233021)

Utilizator David8406Marian David David8406 Data 2 iunie 2024 11:01:17
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>

using namespace std;

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

int n, m, e;
vector<int> v[10005];
bool viz[10005];
int pairst[10005], pairdr[10005], rez;

bool pair_node(int nodst) {
    if (viz[nodst]) return false;
    viz[nodst] = 1;
    for (auto el : v[nodst]) {
        if (pairdr[el] == 0) {
            pairdr[el] = nodst;
            pairst[nodst] = el;
            return 1;
        }
    }
    for (auto el : v[nodst]) {
        if (pair_node(pairdr[el])) {
            pairdr[el] = nodst;
            pairst[nodst] = el;
            return 1;
        }
    }
    return 0;
}

int main(){
    fin >> n >> m >> e;
    for (int i = 1; i <= e; i++) {
        int x, y;
        fin >> x >> y;
        v[x].push_back(y);
    }
    bool change = 1;
    while (change) {
        change = 0;
        for (int i = 1; i <= n; i++) viz[i] = 0;
        for (int i = 1; i <= n; i++) {
            if (pairst[i] == 0 && pair_node(i)) {
                change = 1;
                rez++;
            }
        }
    }
    fout << rez << "\n";
    for (int i = 1; i <= n; i++)
        if (pairst[i] != 0)
            fout << i << " " << pairst[i] << "\n";
}