Cod sursa(job #1656159)

Utilizator wilversingsMarius Aiordachioaei wilversings Data 18 martie 2016 20:37:30
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <cstdio>
#include <bitset>
#include <vector>
#include <cstdlib>
#include <queue>

using namespace std;

const int NMAX = 10002;

vector <int> bgraph[NMAX];

int left[NMAX], right[NMAX], N, M;

bool couple (const int node) {

    for (auto i : bgraph[node])
        if (!right[i]) {
            right[i] = node;
            left[node] = i;
            return true;
        }
    for (auto i : bgraph[node]) {
        if (right[i] != node && couple(right[i])) {
            right[i] = node;
            left[node] = i;
            return true;
        }
    }
    return false;

}

int main () {

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

    int E;

    scanf ("%d%d%d", &N, &M, &E);

    while (E--) {
        int left, right;

        scanf ("%d%d", &left, &right);
        bgraph[left].push_back(right);
    }

    bool stop;
    do {
        stop = true;

        for (int i = 1; i <= N; ++i) {
            if (!left[i] && couple(i)) {
                stop = false;
            }
        }
    } while (!stop);

    int maxCouple = 0;
    for (int i = 1; i <= N; ++i) {
        if (left[i])
            ++maxCouple;
    }
    printf ("%d\n", maxCouple);
    for (int i = 1; i <= N; ++i)
        if (left[i])
            printf ("%d %d\n", i, left[i]);

}