Cod sursa(job #2957353)

Utilizator StanCatalinStanCatalin StanCatalin Data 22 decembrie 2022 14:02:31
Problema Cuplaj maxim in graf bipartit Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.78 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <algorithm>
#include <climits>

using namespace std;

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

const int dim = 20005;

int n,m,e, sursa, destinatie;
vector<pair<int,pair<long long int,int>>> graf_rezidual[dim];
pair<int, int> tata[dim];
int viz[dim];
vector<pair<int,int>> in_destinatie;

int BFS() {
    queue<int> q;
    for (int i=0; i<=m+n+1; i++) {
        viz[i] = 0;
    }
    tata[sursa] = {-1, -1};
    viz[sursa] = 1;
    q.push(sursa);
    while (!q.empty()) {
        int x = q.front();
        q.pop();
    ///    if (x == destinatie) continue;
        for (int i=0; i<graf_rezidual[x].size(); i++) {
            auto y = graf_rezidual[x][i];
            if (y.second.first > 0 && !viz[y.first]) {
                viz[y.first] = 1;
                tata[y.first] = {x, i};
                q.push(y.first);
            }
        }
    }
    return viz[destinatie];
}

void IntorduMuchie(int x, int y) {
    graf_rezidual[x].push_back({y, {1, 0}});
    graf_rezidual[y].push_back({x, {0, 0}});
    graf_rezidual[x][graf_rezidual[x].size() - 1].second.second = graf_rezidual[y].size()-1;
    graf_rezidual[y][graf_rezidual[y].size() - 1].second.second = graf_rezidual[x].size()-1;

    if (y == destinatie) {
        in_destinatie.push_back({x, graf_rezidual[x].size() - 1});
    }
}

int main() {
    in >> n >> m >> e;
    int x, y;
    sursa = 0;
    destinatie = n + m + 1;

    for (int i = 1; i <= n; ++i) {
        IntorduMuchie(sursa, i);
    }

    for (int i=1; i<=m; i++) {
        IntorduMuchie(n + i, destinatie);
    }

    while (e--) {
        in >> x >> y;
        IntorduMuchie(x, n + y);
    }

    long long int maxflow = 0;

    while (BFS()) {
       /// for (auto y:in_destinatie) {
          ///  tata[destinatie] = y;
            pair<int, int> nod = tata[destinatie];
            int catre = destinatie;
            long long int mini = LONG_LONG_MAX;
            while (nod.first != -1) {
                mini = min(mini, graf_rezidual[nod.first][nod.second].second.first);
                nod = tata[nod.first];
            }
            maxflow += mini;
            nod = tata[destinatie];
            while (nod.first != -1) {
                int poz = graf_rezidual[nod.first][nod.second].second.second;
                graf_rezidual[nod.first][nod.second].second.first -= mini;
                graf_rezidual[catre][poz].second.first += mini;
                catre = nod.first;
                nod = tata[nod.first];
      ///      }
        }
    }

    out << maxflow << "\n";
    for (int i=1; i<=n; i++) {
        for (auto y:graf_rezidual[i]) {
            if (y.first != 0 && y.second.first == 0) {
                out << i << " " << y.first - n << "\n";
            }
        }
    }
    return 0;
}