Cod sursa(job #2678870)

Utilizator irimia_alexIrimia Alex irimia_alex Data 28 noiembrie 2020 21:14:14
Problema Cuplaj maxim in graf bipartit Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 10005

using namespace std;

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

vector<int> g[NMAX];
int n,m,e;

int match[NMAX], viz[NMAX];

void read() {
    fin >> n >> m >> e;
    while (e--) {
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
    }
}

int dfs(int u) {
    if (viz[u] == 1) return 0;
    viz[u] = 1;
    for (int v : g[u]) {
        if (match[v] == -1) {
            match[v] = u;
            return 1;
        }
        if (dfs(match[v]) == 1) {
            match[v] = u;
            return 1;
        }
    }
    return 0;
}

int max_matching() {
    int nr = 0;
    for (int i = 1;i <= n;++i) {
        for (int j = 1;j <= m;++j)
            viz[j] = 0;
        if (dfs(i) == 1) ++nr;
    }

    return nr;
}

int main() {
    read();

    for (int i = 1;i <= m;++i)
        match[i] = -1;

    fout << max_matching() << '\n';

    for (int i = 1;i <= m;++i)
        if (match[i] != -1) fout << match[i] << ' ' << i << '\n';

    return 0;
}