Cod sursa(job #1551038)

Utilizator andrei.arnautuAndi Arnautu andrei.arnautu Data 15 decembrie 2015 03:12:21
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
/**
  *  Worg
  */
#include <cstdio>
#include <vector>

using namespace std;
FILE *fin = freopen("cuplaj.in", "r", stdin);
FILE *fout = freopen("cuplaj.out", "w", stdout);

const int MAX_N = 10000;

vector < int > G[1 + MAX_N];
int left[1 + MAX_N], right[1 + MAX_N];
int n, m, e;
bool checked[1 + MAX_N];

int sol;

void readData() {

    scanf("%d%d%d", &n, &m, &e);
    for(int i = 1; i <= e; ++i) {

        int x, y; scanf("%d%d", &x, &y);
        G[x].push_back(y);
    }
}

bool pairUp(int node) {

    if(checked[node])
        return 0;
    checked[node] = true;

    for(int nxt : G[node])
        if(!right[nxt]) {

            left[node] = nxt;
            right[nxt] = node;
            sol++;
            return 1;
        }
    for(int nxt : G[node])
        if(pairUp(right[nxt])) {

            left[node] = nxt;
            right[nxt] = node;
            return 1;
        }

    return 0;
}

int main() {

    readData();

    bool ok;
    do {

        for(int i = 1; i <= n; ++i)
            checked[i] = false;
        ok = false;
        for(int i = 1; i <= n; ++i)
            if(!left[i])
                ok |= pairUp(i);

    }while(ok);

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