Cod sursa(job #3328623)

Utilizator alecsandru121Anghel Alexandru-Mihai alecsandru121 Data 9 decembrie 2025 13:43:42
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 10000;
int capacitate[NMAX + 1][NMAX + 1];
int flux[NMAX + 1][NMAX + 1];
int vis[NMAX + 1], p[NMAX + 1];
vector<int> G[NMAX + 1];
int n_total;

int bfs(int s, int d)
{
    for (int i = 1; i <= n_total; i++)
    {
        vis[i] = 0;
        p[i] = 0;
    }

    queue<int> q;
    q.push(s);
    vis[s] = 1;

    while (!q.empty())
    {
        int nod = q.front();
        q.pop();

        for (auto vecin : G[nod])
        {
            if (!vis[vecin] && capacitate[nod][vecin] - flux[nod][vecin] > 0)
            {
                vis[vecin] = 1;
                p[vecin] = nod;
                q.push(vecin);
            }
        }
    }

    if (!vis[d])
    {
        return 0;
    }

    vector<int> path;
    int x = d;
    while (x != 0 && x != s)
    {
        path.push_back(x);
        x = p[x];
    }
    path.push_back(s);
    reverse(path.begin(), path.end());

    int flow = 1e9;
    for (int i = 0; i < path.size() - 1; i++)
    {
        int a = path[i];
        int b = path[i + 1];
        flow = min(flow, capacitate[a][b] - flux[a][b]);
    }

    for (int i = 0; i < path.size() - 1; i++)
    {
        int a = path[i];
        int b = path[i + 1];
        flux[a][b] += flow;
        flux[b][a] -= flow;