Cod sursa(job #1996975)

Utilizator cosmo0093Raduta Cosmin cosmo0093 Data 3 iulie 2017 07:45:11
Problema Cuplaj maxim in graf bipartit Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <list>

bool visit(int node, std::vector<std::list<int>> &graph, std::vector<bool> &check, std::vector<int> &left, std::vector<int> &right) {
    if (check[node]) {
        return false;
    }
    check[node] = true;

    for (int aux : graph[node]) {
        if (right[aux] < 0 || visit(right[aux], graph, check, left, right)) {
            left[node] = aux;
            right[aux] = node;
            return true;
        }
    }

    return false;
}



int main() {
    std::ifstream in("cuplaj.in");
    std::ofstream out("cuplaj.out");
    int nV, nM, nE;
    in >> nV >> nM >> nE;
    std::vector<std::list<int>> myG(nV + 1);
    std::vector<bool> seen(nV + 1, false);
    std::vector<int> l(nV + 1, -1), r(nV + 1, -1);
    for (int i = 0; i < nE; i++)
    {
        int a, b;
        in >> a >> b;
        myG[a].push_back(b);
    }
    in.close();
    bool ok = true;
    while (ok)
    {
        ok = false;
        for (int i = 0; i <= nV; i++)
        {
            seen[i] = false;
        }
        for (int i = 1; i <= nV; i++)
        {
            if (l[i] < 0)
            {
                if (visit(i, myG, seen, l, r))
                {
                    ok = true;
                }
            }
        }
    }
    int sum = 0;
    for (int i = 1; i <= nV; i++)
    {
        if (l[i] > 0)
        {
            sum++;
        }
    }
    out << sum << '\n';
    for (int i = 1; i <= nV; i++)
    {
        if (l[i] > 0)
        {
            out << i << ' ' << l[i] << '\n';
        }
    }
    out.close();
    return 0;
}