Cod sursa(job #2585857)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 19 martie 2020 14:38:24
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 10005;

vector<int> graf[MAXN];
int cuplajst[MAXN], cuplajdr[MAXN];
bool viz[MAXN];

bool cuplaj(int nod){
    if(viz[nod]) return 0;
    viz[nod] = 1;
    for(auto x: graf[nod]){
        if(cuplajst[x] == 0){
            cuplajdr[nod] = x;
            cuplajst[x] = nod;
            return 1;
        }
    }
    for(auto x: graf[nod]){
        if(cuplaj(cuplajst[x])){
            cuplajdr[nod] = x;
            cuplajst[x] = nod;
            return 1;
        }
    }
    return 0;
}

int main()
{
    ifstream fin("cuplaj.in");
    ofstream fout("cuplaj.out");
    int n, m, e;
    fin >> n >> m >> e;
    for(int i = 1; i <= e; ++i){
        int x, y;
        fin >> x >> y;
        graf[x].push_back(y);
    }
    bool done = 0;
    while(!done){
        memset(viz, 0, n + 1);
        done = 1;
        for(int i = 1; i <= n; ++i)
            if(cuplajdr[i] == 0 && cuplaj(i)) done = 0;
    }
    int ans = 0;
    for(int i = 1; i <= n; ++i)
        if(cuplajdr[i] > 0) ans++;
    fout << ans << "\n";
    for(int i = 1; i <= n; ++i){
        if(cuplajdr[i] > 0)
            fout << i << " " << cuplajdr[i] << "\n";
    }
    return 0;
}