Cod sursa(job #1093568)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 28 ianuarie 2014 11:44:03
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

const int MAXN = 1e4 + 5;

typedef vector <int> :: iterator It;

vector <int> G[MAXN];
bitset <MAXN> Used;
int match[MAXN], per[MAXN];
int L, R, M;

bool pairUp(const int &Node) {
    if(Used[Node])
        return false;
    Used[Node] = true;
    for(It it = G[Node].begin(), fin = G[Node].end();  it != fin ; ++ it)
        if(!per[*it] || pairUp(per[*it])) {
            per[*it] = Node;
            match[Node] = *it;
            return true;
        }
    return false;
}

int main() {
    ifstream fin("cuplaj.in");
    ofstream fout("cuplaj.out");
    fin >> L >> R >> M;
    for(int i = 1 ; i <= M ; ++ i) {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
    }
    int Ans = 0;
    for(bool change = true ; change ; ) {
        change = false;
        Used.reset();
        for(int i = 1 ; i <= L ; ++ i)
            if(!match[i])
                if(pairUp(i)) {
                    ++ Ans;
                    change = true;
                }
    }
    fout << Ans << '\n';
    for(int i = 1 ; i <= L ; ++ i)
        if(match[i])
            fout << i << ' ' << match[i] << '\n';
    return 0;
}