Cod sursa(job #1413835)

Utilizator FlameingoAiordachioaei Marius Flameingo Data 2 aprilie 2015 09:48:18
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <cstdio>
#include <bitset>
#include <vector>
using namespace std;

const int NMAX = 10003;

vector <int> BG[NMAX];
bitset <NMAX> check;
int left[NMAX], right[NMAX];

bool pairup (const int &node) {

    if (check[node])
        return 0;
    check[node] = 1;
    vector <int> :: iterator i;
    for (i = BG[node].begin (); i != BG[node].end (); ++i)
        if (!right[*i]) {
            left[node] = *i;
            right[*i] = node;
            return 1;
        }
    for (i = BG[node].begin (); i != BG[node].end (); ++i)
        if (pairup (right[*i])) {
            left[node] = *i;
            right[*i] = node;
            return 1;
        }
    return 0;

}

int main () {

    freopen ("cuplaj.in", "r", stdin);
    freopen ("cuplaj.out", "w", stdout);
    int N, M, E, a, b, i;
    scanf ("%d%d%d", &N, &M, &E);
    while (E--) {
        scanf ("%d%d", &a, &b);
        BG[a].push_back (b);
    }
    bool t;
    do {
        t = 0;
        check.reset ();
        for (i = 1; i <= N; ++i)
            if (!left[i])
                t |= pairup (i);
    }
    while (t);
    int ans = 0;
    for (i = 1; i <= N; ++i)
        ans += left[i] != 0;
    printf ("%d\n", ans);
    for (i = 1; i <= N; ++i)
    if (left[i]) {
        printf ("%d %d\n", i, left[i]);
    }
}