Cod sursa(job #2955115)

Utilizator IRadu1529Radu Ionescu IRadu1529 Data 16 decembrie 2022 11:57:25
Problema Cuplaj maxim in graf bipartit Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.77 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <fstream>
#include <set>
using namespace std;


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

#define N 10001

int n, m, e, maxFlow;
int s, t;
int pairOfA[N];
int capacity[N][N];
vector<vector<int>> l(N, vector<int>());
vector<int> parent(N);
set<pair<int, int>> edges;
//O(N * M2)
int bfs(int s, int t) {
    parent[s] = -2; // orice != -1 si [0,n+m+1]
    queue<pair<int, int>> q;
    q.push({ s, 1e9 });

    while (!q.empty()) {
        int cur = q.front().first;
        int maxFlow = q.front().second;
        q.pop();

        for (int next : l[cur]) {
            if (parent[next] == -1 && capacity[cur][next]) {
                parent[next] = cur;
                int bottleneck = min(maxFlow, capacity[cur][next]);
                if (next == t)
                    return bottleneck;
                q.push({ next, bottleneck });
            }
        }
    }

    return 0;
}

void read() {
    fin >> n >> m >> e;

    while (e--) {
        int a, b;
        fin >> a >> b; // muchia a -> n+b
        l[a].push_back(n + b);
        l[n + b].push_back(a);
        capacity[a][n + b] = 1; // muchia de inaintare 
    }
}

int main() {

    read();

    s = 0; t = n + m + 1;

    for (int i = 1; i <= n; i++) { // concectam sursa la nodurile din A
        l[s].push_back(i);
        l[i].push_back(s);
        capacity[s][i] = 1;
    }

    for (int i = n + 1; i <= n + m; i++) { // concectam nodurile din B la destinatie
        l[i].push_back(t);
        l[t].push_back(i);
        capacity[i][t] = 1;
    }

    for (int i = 0; i <= n + m + 1; i++) // resetam parintii
        parent[i] = -1;

    int bottleneck;

    while (bottleneck = bfs(s, t)) { // cat mai sunt path uri care mai accepta flux
        maxFlow += bottleneck;
        int node = t;
        
        while (node != s) { // mergem inapoi pe path ul gasit 
            
            int prev = parent[node];
           // cout << prev << " " << node << "\n";                
           
            capacity[prev][node] -= bottleneck; // muchia de inaintare pierde din spatiu exact bottleneck
            capacity[node][prev] += bottleneck; // muchia de retur poate face undo cu exact bottleneck mai mult

            if (node > s && node < t && prev > s && prev < t)
                pairOfA[prev] = node;
                
            node = prev;
        }
        //cout << "\n\n";

        for (int i = 0; i <= n + m + 1; i++) // resetam parintii
            parent[i] = -1;        
    }

    fout << maxFlow << "\n";

    for (int i = 1; i <= n; i++)
        if (pairOfA[i])
            fout << i << " " << pairOfA[i] - n << "\n";
    return 0;
}