Cod sursa(job #2585851)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 19 martie 2020 14:34:19
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 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 notdone = 1;
    while(notdone){
        memset(viz, 0, n + 1);
        notdone = 0;
        for(int i = 1; i <= n; ++i)
            if(!cuplajdr[i]) notdone |= cuplaj(i);
    }
    int ans = 0;
    for(int i = 1; i <= n; ++i)
        if(cuplajdr[i]) ans++;
    fout << ans << "\n";
    for(int i = 1; i <= n; ++i){
        if(cuplajdr[i])
            fout << i << " " << cuplajdr[i] << "\n";
    }
    return 0;
}