Cod sursa(job #2207311)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 25 mai 2018 15:02:54
Problema Cuplaj maxim in graf bipartit Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <bits/stdc++.h>

using namespace std;

int n1, n2, m, n, s, t;
vector < int > V[20005];
vector < vector < int > > C, F;

int Flux(){
    int maxFlow = 0;

    while (1){
        vector < int > Dad(10005, -1);
        queue < int > Q;

        Q.push(s);
        Dad[s] = -2;

        while (Q.size()){
            int node = Q.front(); Q.pop();

            for (auto it : V[node]){
                if (Dad[it] == -1 && C[node][it] > F[node][it]){
                    Dad[it] = node;
                    Q.push(it);
                }
            }
        }

        if (Dad[t] == -1) return maxFlow;
        
        int y = t;

        y = t;
        while (y != s){
            F[Dad[y]][y]++;
            F[y][Dad[y]]--;
            y = Dad[y];
        }

        maxFlow++;
    }
}

int main(){
    ifstream cin("cuplaj.in");
    ofstream cout("cuplaj.out");
    cin >> n1 >> n2 >> m;

    C.resize(n1 + n2 + 2);
    for (auto &it: C) it.resize(n1 + n2 + 2);
    F.resize(n1 + n2 + 2);
    for (auto &it: F) it.resize(n1 + n2 + 2);

    n = n1 + n2;
    s = 0; t = n + 1;

    for (int i = 1, x, y; i <= m; i++){
        cin >> x >> y;
        V[x].push_back(y + n1);
        V[y + n1].push_back(x);

        V[s].push_back(x);
        V[y + n1].push_back(t);

        C[s][x] = 1;
        C[y + n1][t] = 1; 
        C[x][y + n1] = 1;
    }

    int mx = Flux();

    cout << mx << "\n";
    for (int i = 1; i <= n; i++)
        for (auto it : V[i])
            if (F[i][it] == 1 && i != s && it != t)
                cout << i << " " << it - n1 << "\n";
    return 0;
}