Cod sursa(job #1436571)

Utilizator OpportunityVlad Negura Opportunity Data 16 mai 2015 01:44:01
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#include <vector>
using namespace std;
 
ifstream fi("cuplaj.in");
ofstream fo("cuplaj.out");
 
int n,m,v,viz[10001],x,y,i,j,rs,l[10001],r[10001];
vector <int> g[10001];
 
int cuplaj(int nod) {
    if (viz[nod]) {
        return 0;
    }
    viz[nod] = 1;
 
    for (vector <int>::iterator it = g[nod].begin(); it != g[nod].end(); it++) {
        if (!r[*it] || cuplaj(r[*it])) {
            l[nod] = *it;
            r[*it] = nod;
            return 1;
        }
    }
 
    return 0;
}
 
int main() {
 
    fi >> n >> m >> v;
 
    for (i = 0; i < v; i++) {
        fi >> x >> y;
        g[x].push_back(y);
    }
 
    int ok = 1;
    while (ok) {
        ok = 0;
        for (i = 1; i <= n; i++) {
            viz[i] = 0;
        }
 
        for (i = 1; i <= n; i++) {
            if (!l[i] && cuplaj(i)) {
                rs++;
                ok = 1;
            }
        }
    }
 
    fo << rs << endl;
 
    for (i = 1; i <= n; i++) {
        if (l[i]) {
            fo << i << ' ' << l[i] << '\n';
        }
    }
 
    return 0;
}