Cod sursa(job #1218792)

Utilizator mariusn01Marius Nicoli mariusn01 Data 12 august 2014 15:54:10
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <fstream>
#include <vector>
#include <bitset>

#define DIM 10002

using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

vector<int> L[DIM];
bitset<DIM> V;

int Left[DIM], Right[DIM];

int n, m, k, x, y, i, sol, ok;

int cupleaza(int nod) {
    V[nod] = 1;
    // caut un vecin d-al sau necuplat
    for (int i = 0; i<L[nod].size();i++) {
        int x = L[nod][i];
        if (Right[x] == 0) {
            Left[nod] = x;
            Right[x] = nod;
            return 1;
        }
    }

     for (int i = 0; i<L[nod].size();i++) {
        int x = L[nod][i];
        if (V[Right[x]] == 0 && cupleaza(Right[x])) {
            Left[nod] = x;
            Right[x] = nod;
            return 1;
        }
    }

    return 0;
}

int main() {
    fin>>n>>m;
    fin>>k;
    for (i=1;i<=k;i++) {
        fin>>x>>y;
        L[x].push_back(y);
    }
    do {

        for (i=1;i<=n;i++)
            V[i] = 0;

        ok = 0;
        for (i=1;i<=n;i++)
            if (Left[i] == 0 && cupleaza(i)) {
                ok = 1;
            }

    } while (ok);


    for (i=1;i<=n;i++) {
        if (Left[i])
            sol++;
    }

    fout<<sol<<"\n";
    for (i=1;i<=n;i++)
        if (Left[i]) {
            fout<<i<<" "<<Left[i]<<"\n";
        }
    return 0;
}