Cod sursa(job #2497580)

Utilizator ContDeRacistAliniateEBlat ContDeRacist Data 22 noiembrie 2019 21:14:55
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 1e4 + 7;

namespace Cuplaj {

    int n;
    int st[N], dr[N];
    bool viz[N];
    vector < int > adia[N];

    void AddEdge(int x, int y) {
        adia[x].push_back(y);
    }

    bool cuplaj(int nod) {
        viz[nod] = 1;
        for (int i : adia[nod]) {
            if (!st[i]) {
                st[i] = nod;
                dr[nod] = i;
                return 1;
            }
        }
        for (int i : adia[nod]) {
            if (!viz[st[i]] && cuplaj(st[i])) {
                st[i] = nod;
                dr[nod] = i;
                return 1;
            }
        }
        return 0;
    }

    int Build(int _n) {
        n = _n;
        bool act(1);
        int ans(0);
        while (act) {
            act = 0;
            fill(viz + 1, viz + n + 1, 0);
            for (int i = 1; i <= n; ++i)
                if (!viz[i] && !dr[i] && cuplaj(i))
                    ++ans, act = 1;
        }
        return ans;
    }
}

int main()
{
    int n, m, k, x, y;
    fin >> n >> m >> k;
    while (k--) {
        fin >> x >> y;
        Cuplaj::AddEdge(x, y);
    }
    fout << Cuplaj::Build(n) << '\n';
    vector < pair < int, int > > ans;
    for (int i = 1; i <= n; ++i)
        if (Cuplaj::dr[i])
            ans.push_back({i, Cuplaj::dr[i]});
    for (auto i : ans)
        fout << i.first << ' ' << i.second << '\n';
    return 0;
}