Cod sursa(job #902937)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 1 martie 2013 17:34:17
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
#include <cstdio>
#include <cstring>
#include <cassert>

#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>

using namespace std;

typedef long long LL;
typedef vector<int>::iterator it;

const int oo = 0x3f3f3f3f;
const int MAX_N = 10005;
const int NIL = -1;

vector<int> G[MAX_N];
int N, Pair[2 * MAX_N], MaxMatch;
bool Used[MAX_N];

int PairUp(int X) {
    if (Used[X])
        return 0;
    Used[X] = true;
    for (it Y = G[X].begin(); Y != G[X].end(); ++Y) {
        if (Pair[*Y] == NIL) {
            Pair[X] = *Y; Pair[*Y] = X;
            return 1;
        }
    }
    for (it Y = G[X].begin(); Y != G[X].end(); ++Y) {
        if (PairUp(Pair[*Y])) {
            Pair[X] = *Y; Pair[*Y] = X;
            return 1;
        }
    }
    return 0;
}

void MaximumMatching() {
    memset(Pair, NIL, sizeof(Pair));
    for (int Change = 1; Change != 0; ) {
        memset(Used, 0, sizeof(Used));
        Change = 0;
        for (int X = 1; X <= N; ++X)
            if (Pair[X] == NIL)
                Change |= PairUp(X);
    }
    for (int X = 1; X <= N; ++X)
        if (Pair[X] != NIL)
            ++MaxMatch;
}

void Solve() {
    MaximumMatching();
}

void Read() {
    assert(freopen("cuplaj.in", "r", stdin));
    int M, E; assert(scanf("%d %d %d", &N, &M, &E) == 3);
    for (; E > 0; --E) {
        int X, Y; assert(scanf("%d %d", &X, &Y) == 2);
        G[X].push_back(Y + N);
    }
}

void Print() {
    assert(freopen("cuplaj.out", "w", stdout));
    printf("%d\n", MaxMatch);
    for (int X = 1; X <= N; ++X)
        if (Pair[X] != NIL)
            printf("%d %d\n", X, Pair[X] - N);
}

int main() {
    Read();
    Solve();
    Print();
    return 0;
}