Cod sursa(job #1414053)

Utilizator harababurelPuscas Sergiu harababurel Data 2 aprilie 2015 12:13:15
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#define nmax 10001
using namespace std;

vector <int> v[nmax];
bool used[nmax];
int nL, nR, m, x, y, sol;
int L[nmax], R[nmax];

bool match(int x) {
    if(used[x]) return false;
    used[x] = true;

    for(auto y:v[x])
        if(!R[y] || match(R[y])) {
            L[x] = y;
            R[y] = x;
            return true;
        }

    return false;
}


int main() {
    ifstream f("cuplaj.in");
    ofstream g("cuplaj.out");

    f>>nL>>nR>>m;
    for(int i=1; i<=m; i++) {
        f>>x>>y;
        v[x].push_back(y);
    }

    bool worth = true;
    while(worth) {
        worth = false;
        memset(used, 0, sizeof(used));

        for(int i=1; i<=nL; i++)
            if(!L[i] && match(i)) {
                sol++;
                worth = true;
            }
    }

    g<<sol<<"\n";
    for(int i=1; i<=nL; i++)
        if(L[i]) g<<i<<" "<<L[i]<<"\n";

    return 0;
}