Cod sursa(job #2891715)

Utilizator LucaMihaiLM10Luca Ilie LucaMihaiLM10 Data 19 aprilie 2022 16:24:21
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.46 kb
#include <bits/stdc++.h>

#define MAX_N 10000
#define MAX_M 10000
#define MULT (1 << 30)
#define NIL 0

using namespace std;

struct MATCH {
    int n, m;
    int pairU[MAX_N + 1], pairV[MAX_M + 1], dist[MAX_N + 1];
    vector <int> edges[MAX_N + 1];
    vector <pair<int, int>> match;

    void add_edge( int u, int v ) {
        edges[u].push_back( v );
    }

    bool bfs() {
        int u;
        queue <int> q;

        dist[NIL] = MULT;
        for ( u = 1; u <= n; u++ ) {
            if ( pairU[u] == NIL ) {
                dist[u] = 0;
                q.push( u );
            } else
                dist[u] = MULT;
        }

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

            if ( dist[u] < dist[NIL] ) {
                for ( int v: edges[u] ) {
                    if ( dist[pairV[v]] == MULT ) {
                        dist[pairV[v]] = dist[u] + 1;
                        q.push( pairV[v] );
                    }
                }
            }
        }

        return dist[NIL] != MULT;
    }

    bool dfs( int u ) {
        if ( u == NIL )
            return true;

        for ( int v: edges[u] ) {
            if ( dist[pairV[v]] == dist[u] + 1 && dfs( pairV[v] ) ) {
                pairU[u] = v;
                pairV[v] = u;
                return true;
            }
        }

        dist[u] = MULT;

        return false;
    }

    void maxMatch() {
        int maxMatch, u, v;

        for ( u = 0; u <= n; u++ )
            pairU[u] = NIL;
        for ( v = 0; v <= m; v++ )
            pairV[v] = NIL;


        maxMatch = 0;
        while ( bfs() ) {
            for ( u = 1; u <= n; u++ ) {
                if ( pairU[u] == NIL && dfs( u ) )
                    maxMatch++;
            }
        }

        for ( u = 1; u <= n; u++ ) {
            if ( pairU[u] != NIL )
                match.push_back( { u, pairU[u] } );
        }
    }
};

MATCH match;

int main() {
    ifstream cin( "cuplaj.in" );
    ofstream cout( "cuplaj.out" );

    int n, m, k, u, v, i;

    cin >> n >> m >> k;
    match.n = n;
    match.m = m;
    for ( i = 0; i < k; i++ ) {
        cin >> u >> v;
        match.add_edge( u, v );
    }

    match.maxMatch();

    cout << match.match.size() << "\n";
    for ( i = 0; i < match.match.size(); i++ )
        cout << match.match[i].first << " " << match.match[i].second << "\n";

    return 0;
}