Cod sursa(job #2419190)

Utilizator radugheoRadu Mihai Gheorghe radugheo Data 7 mai 2019 19:17:33
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>
#define DIM 100005

using namespace std;

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

int n, m, e, i, x, y, ok, sol;
int st[DIM], dr[DIM], f[DIM];

vector <int> g[DIM];

int cupleaza (int nod){
    int i, vecin;
    if (f[nod] == 1){
        return 0;
    }
    f[nod] = 1;
    for (i=0; i<g[nod].size(); i++){
        vecin = g[nod][i];
        if (dr[vecin] == 0){
            st[nod] = vecin;
            dr[vecin] = nod;
            sol++;
            return 1;
        }
    }
    for (i=0; i<g[nod].size(); i++){
        vecin = g[nod][i];
        if (cupleaza(dr[vecin])){
            st[nod] = vecin;
            dr[vecin] = nod;
            return 1;
        }
    }
    return 0;
}

int main(){
    fin >> n >> m >> e;
    for (i=1; i<=e; i++){
        fin >> x >> y;
        g[x].push_back(y);
    }
    do{
        ok = 0;
        for (i=0; i<=DIM; i++)
            f[i] = 0;
        for (i=1; i<=n; i++){
            if (st[i] == 0){
                ok |= cupleaza (i);
            }
        }
    } while (ok == 1);
    fout << sol << "\n";
    for (i=1; i<=n; i++){
        if (st[i] != 0){
            fout << i << " " << st[i] << "\n";
        }
    }
    return 0;
}