Cod sursa(job #2198966)

Utilizator Chirita_MateiChirita Matei Chirita_Matei Data 25 aprilie 2018 23:11:55
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>

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

const int NMAX = 10010;

vector<int> G[NMAX];
int r[NMAX];
int l[NMAX];
int v[NMAX];

bool match(int node){
    if(v[node]) return 0;
    v[node] = 1;

    for(auto next : G[node]){
        if(!r[next]){
            r[next] = node;
            l[node] = next;
            return 1;
        }
    }

    for(auto next : G[node]){
        if(match(r[next])){
            r[next] = node;
            l[node] = next;
            return 1;
        }
    }

    return 0;
}

int main()
{
    int x, y, n, m, e;
    fin >> n >> m >> e;

    for(int i = 1; i <= e; i++){
        fin >> x >> y;
        G[x].push_back(y);
    }

    bool ok;
    do{
        ok = false;
        memset(v, 0, sizeof(v));
        for(int i = 1; i <= n; i++){
            if(!l[i] && !v[i]) ok |= match(i);
        }
    }while(ok);

    int cuplaj = 0;
    for(int i = 1; i <= n; i++) cuplaj += (l[i] > 0);

    fout << cuplaj << '\n';
    for(int i = 1; i <= n; i++) if(l[i]) fout << i << ' ' << l[i] << '\n';

    return 0;
}