Cod sursa(job #2475822)

Utilizator gabib97Gabriel Boroghina gabib97 Data 17 octombrie 2019 17:15:27
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <vector>
#include <cstring>

#define N 10005

using namespace std;

int n, m, e, l[N], r[N];
bool o[N];
vector<int> G[N];

int match(int s) {
    if (o[s]) return 0;
    o[s] = true;

    for (int x : G[s])
        if (!r[x]) {
            l[s] = x;
            r[x] = s;
            return 1;
        }
    for (int x : G[s])
        if (match(r[x])) {
            l[s] = x;
            r[x] = s;
            return 1;
        }
    return 0;
}

int main() {
    ifstream cin("cuplaj.in");
    ofstream cout("cuplaj.out");
    cin >> n >> m >> e;
    int x, y;
    while (e--) {
        cin >> x >> y;
        G[x].push_back(y);
    }

    int matching;
    do {
        memset(o, 0, sizeof(o));
        matching = 0;
        for (int i = 1; i <= n; i++)
            if (!l[i])
                matching += match(i);
    } while (matching);

    matching = 0;
    for (int i = 1; i <= n; i++)
        if (l[i]) matching++;

    cout << matching << '\n';
    for (int i = 1; i <= n; i++)
        if (l[i])
            cout << i << " " << l[i] << '\n';
    return 0;
}