Cod sursa(job #2430485)

Utilizator freak93Adrian Budau freak93 Data 15 iunie 2019 00:41:26
Problema Cuplaj maxim in graf bipartit Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cassert>
#include <cstring>
#include <cstdio>

using namespace std;

int m_used[10005];
int m_left[10005];
int m_right[10005];
vector<int> m_edges[10006];

int N, M, E;

bool go(int node) {
    m_used[node] = 1;

    for (auto &next : m_edges[node])
        if (!m_right[next] || (!m_used[m_right[next]] && go(m_right[next]))) {
            m_left[node] = next;
            m_right[next] = node;
            return true;
        }
    return false;
}

int main() {
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);

    ios::sync_with_stdio(false);

    //ifstream cin("cuplaj.in");
    //ofstream cout("cuplaj.out");

    cin >> N >> M >> E;
    for (int i = 0; i < E; ++i) {
        int x, y; cin >> x >> y;
        m_edges[x].emplace_back(y);
    }

    while (true) {
        memset(m_used, 0, sizeof(m_used));
        bool try_again = false;
        for (int i = 1; i <= N; ++i)
            if (!m_used[i] && !m_left[i])
                try_again = (try_again || go(i));
        if (!try_again)
            break;
    }

    vector< pair<int, int> > match;
    for (int i = 1; i <= N; ++i)
        if (m_left[i])
            match.emplace_back(i, m_left[i]);

    cout << match.size() << "\n";
    for (auto &p : match)
        cout << p.first << " " << p.second << "\n";
}