Cod sursa(job #2366814)

Utilizator qwerty1234qwerty qwerty1234 Data 4 martie 2019 22:20:17
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb

#include <bits/stdc++.h>


using namespace std;

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

const int Nmax = 1e4 + 5;

int a[Nmax] , f , n  , m , st[Nmax] , dr[Nmax] , ans;

vector < int > L[Nmax];

bitset < Nmax > viz;

bool ok;

inline bool Cuplaj(int nod)
{
    if(viz[nod])
        return false;
    viz[nod] = 1;
    for(auto it : L[nod])
        if(!dr[it])
    {
        st[nod] = it;
        dr[it] = nod;
        return true;
    }
    for(auto it : L[nod])
        if(dr[it] && Cuplaj(dr[it]))
    {
        st[nod] = it;
        dr[it] = nod;
        return true;
    }
    return false;
}

int main()
{
    int x , y;
    fin >> f >> n >> m;
    for(int i = 1 ; i <= m ; i++)
        fin >> x >> y , L[x].push_back(y);
    ok = true;
    while(ok)
    {
        ok = false;
        viz.reset();
        for(int i = 1 ; i <= f ; i++)
            if(!st[i] && Cuplaj(i))
                ++ans , ok = true;
    }
    fout << ans << "\n";
    for(int i = 1 ; i <= f ; i++)
        if(st[i])
            fout << i << " " << st[i] << "\n";

    fin.close();
    fout.close();
}