Cod sursa(job #2116366)

Utilizator amaliarebAmalia Rebegea amaliareb Data 27 ianuarie 2018 16:02:35
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
const int MaxN = 10005;
int A[MaxN], B[MaxN], n, m, e;
vector<int> gr[MaxN];
bitset<MaxN> viz;

bool dfs(int node) {
    viz[node] = 1;
    for (auto son: gr[node]) if (!B[son]) {
        B[son] = node;
        A[node] = son;
        return 1;
    }
    for (auto son: gr[node]) if(!viz[B[son]] && dfs(B[son])) {
        A[node] = son;
        B[son] = node;
        return 1;
    }
    return 0;
}

int main()
{
    f >> n >> m >> e;
    for (int i = 1; i <= e; ++i) {
        int x, y;
        f >> x >> y;
        gr[x].push_back(y);
    }
    int add = 1, ans = 0;
    while (add) {
        viz.reset();
        add = 0;
        for (int i = 1; i <= n; ++i) if (!viz[i] && !A[i] && dfs(i)) ++add;
        ans += add;
    }
    g << ans << '\n';
    for (int i = 1; i <= n; ++i) if(A[i]) g << i << ' ' << A[i] << '\n';
    return 0;
}