Cod sursa(job #2986471)

Utilizator Chris_BlackBlaga Cristian Chris_Black Data 28 februarie 2023 17:36:18
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");
const int N = 10009;
int n,m,e,a,b;
vector<vector<int>> G;
int L[N],R[N];
bitset<N> v;

int CuplajMaxim();
bool DoMatch(int x);

int main()
{
    cin>>n>>m>>e;
    G = vector<vector<int>>(n+m+3);
    while(e--)
    {
        cin>>a>>b;
        G[a].push_back(b);
    }

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

int CuplajMaxim()
{
    int maxmatch = 0;
    bool found_path = false;
    do
    {
        v.reset();
        found_path = false;
        for(int i = 1; i <= n; ++i)
            if(!L[i] && DoMatch(i))
            {
                found_path = true;
                maxmatch++;
            }
    }while(found_path);

    return maxmatch;
}
bool DoMatch(int x)
{
    if( v[x] )return false;
    v[x] = true;
    for(auto y : G[x])
        if(!R[y] || DoMatch(R[y]))
        {
            R[y] = x;
            L[x] = y;
            return true;
        }
    return false;
}