Cod sursa(job #2134681)

Utilizator matdibuDibu Matei matdibu Data 18 februarie 2018 11:18:36
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m, e, st[10003], dr[10003];
vector<int> L[10003];
int cuplajMax;
int viz[10003];

int Cupleaza(int k) {
    if(viz[k])
        return 0;
    viz[k] = 1;
    for(auto i : L[k])
        if(dr[i] == 0) {
            st[k] = i;
            dr[i] = k;
            return 1;
        }
    for(auto i : L[k])
        if(Cupleaza(dr[i])) {
            st[k] = i;
            dr[i] = k;
            return 1;
        }
    return 0;
}

void citire() {
    int i, x, y;
    f >> n >> m >> e;
    for(i=1; i<=e; i++) {
        f >> x >> y;
        L[x].push_back(y);
    }
    f.close();
}

void rezolvare() {
    int i, gata = 0;
    while(!gata) {
        gata = 1;
        for(i=1; i<=n; i++)
            viz[i] = 0;
        for(i=1; i<=n; i++)
            if(st[i] == 0 && Cupleaza(i)) {
                cuplajMax++;
                gata = 0;
            }
    }
    g << cuplajMax << '\n';
    for(i=1; i<=n; i++)
        if(st[i])
            g << i << ' ' << st[i] << '\n';
    g<<'\n';
    g.close();
}

int main() {
    citire();
    rezolvare();
    return 0;
}