Cod sursa(job #1896351)

Utilizator preda.andreiPreda Andrei preda.andrei Data 28 februarie 2017 17:18:28
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.76 kb
#include <fstream>
#include <vector>

using namespace std;

using Graph = vector<vector<int>>;

bool FindPair(const Graph &g, vector<bool> &vis, vector<int> &so1, vector<int> &so2, int node)
{
    if (vis[node]) {
        return false;
    }
    vis[node] = true;

    for (int next : g[node]) {
        if (so2[next] == -1) {
            so1[node] = next;
            so2[next] = node;
            return true;
        }
    }

    for (int next : g[node]) {
        if (FindPair(g, vis, so1, so2, so2[next])) {
            so1[node] = next;
            so2[next] = node;
            return true;
        }
    }
    return false;
}

vector<pair<int, int>> FindMaxMatching(const Graph &g, int m)
{
    vector<int> so1(g.size(), -1);
    vector<int> so2(m, -1);

    bool changes = false;
    do {
        changes = false;
        vector<bool> visited(g.size(), false);

        for (unsigned i = 0; i < g.size(); ++i) {
            if (so1[i] == -1 && FindPair(g, visited, so1, so2, i)) {
                changes = true;
            }
        }
    } while (changes);

    vector<pair<int, int>> matching;
    for (unsigned i = 0; i < g.size(); ++i) {
        if (so1[i] != -1) {
            matching.push_back({i, so1[i]});
        }
    }
    return matching;
}

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

    int n, m, k;
    fin >> n >> m >> k;

    Graph graph(n);
    while (k--) {
        int x, y;
        fin >> x >> y;
        graph[x - 1].push_back(y - 1);
    }

    auto matching = FindMaxMatching(graph, m);
    fout << matching.size() << "\n";
    for (const auto &edge : matching) {
        fout << edge.first + 1 << " " << edge.second + 1 << "\n";
    }

    return 0;
}