Cod sursa(job #1973987)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 26 aprilie 2017 16:20:12
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>


using namespace std;
const int Nmax = 26013;
int Left[Nmax], Right[Nmax], L, R, E;
vector<vector<int> > G;
bitset<Nmax> used;

bool match(int k) {
    if(used[k])
        return false;
    used[k] = true;
    for(auto it : G[k])
        if(Left[it] == 0) {
            Left[it] = k;
            Right[k] = it;
            return true;
        }

    for(auto it : G[k])
        if(match(Left[it])) {
            Right[k] = it;
            Left[it] = k;
            return true;
        }
    return false;
}

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

    ios::sync_with_stdio(false);

    cin >> L >> R >> E;
    G.resize(L + R + 1);
    for(int i = 1; i <= E; ++i) {
        int a,b;
        cin >> a >> b;
        G[a].push_back(L + b);
        G[L + b].push_back(a);
    }

    int cuplaj = 0;
    bool updated = true;

    while(updated) {
        updated = false;
        used = 0;
        for(int i = 1; i <= L; ++i)
            if(!Right[i] && match(i)) {
                updated = true;
                    cuplaj += 1;
            }
    }
    cout << cuplaj << "\n";
    for(int i = 1; i <= L; ++i)
        if(Right[i])
            cout << i << " " << Right[i] - L << "\n";

    return 0;
}