Cod sursa(job #3152989)

Utilizator Chris_BlackBlaga Cristian Chris_Black Data 27 septembrie 2023 15:49:49
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>

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

const int N = 10009;

int n, m, e, u, v, L[N], R[N];
vector<vector<int>> G;
bool viz[N];

bool DoMatch(int x)
{
    if(viz[x])return false;
    viz[x] = true;

    for(auto y : G[x])
        if(!R[y])
        {
            L[x] = y;
            R[y] = x;
            return true;
        }

    for(auto y : G[x])
        if(DoMatch(R[y]))
        {
            L[x] = y;
            R[y] = x;
            return true;
        }


    return false;
}
int MaxMatching()
{
    int max_matching = 0;
    for(bool found_match = true; found_match;)
    {
        found_match = false;

        fill(viz + 1, viz + n + 1, false);
        for(int i = 1; i <= n; ++i)
            if(!L[i] && DoMatch(i))
                max_matching += found_match = true;
    }

    return max_matching;
}

int main()
{
    cin >> n >> m >> e;
    G = vector<vector<int>>(n + 1);
    for(int i = 1; i <= e; ++i)
    {
        cin >> u >> v;
        G[u].push_back(v);
    }

    cout << MaxMatching() << '\n';
    for(int i = 1; i <= n; ++i)
        if(L[i])
            cout << i << ' ' << L[i] << '\n';
    return 0;
}