Cod sursa(job #2856137)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 23 februarie 2022 14:04:27
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>
using namespace std;

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

int n,m,e;
int x,y;
vector < int > v[10005];
bool ok=1,viz[10005];
int l[10005],r[10005];
int ctr;

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

bool dfs(int nod) {
    viz[nod]=1;
    for (auto k:v[nod]) {
        if (r[k]==0 || (viz[r[k]]==0 && dfs(r[k])==1)) {
            r[k] = nod;
            l[nod] = k;
            return 1;
        }
    }
    return 0;
}

void solve() {
    while (ok) {
        ok=0;
        fill(viz+1,viz+n+1,0);
        for (int i=1;i<=n;i++) {
            if (l[i]==0) {
                if (dfs(i)) {
                    ctr++;
                    ok=1;
                }
            }
        }
    }
}

void show() {
    g << ctr << '\n';
    for (int i=1;i<=n;i++) {
        if (l[i]) {
            g << i << " " << l[i] << '\n';
        }
    }
}

int main()
{
    read();
    solve();
    show();
    return 0;
}