Cod sursa(job #969715)

Utilizator narcis_vsGemene Narcis - Gabriel narcis_vs Data 5 iulie 2013 10:22:28
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
///algoritmului lui Hopcroft Karp
/// complexitate: O(sqrt(N)*M)
#include <fstream>
#include <cstring>
#include <bitset>
#include <vector>

#define In "cuplaj.in"
#define Out "cuplaj.out"
#define Nmax 10020

using namespace std;
vector<int>Graph[Nmax];

int N, M, Left[Nmax], Right[Nmax];
bool use[Nmax];

inline void Read()
{
    int E, X, Y;
    ifstream f(In);
    f>>N>>M>>E;
    while(E--)
    {
        f>>X>>Y;
        Graph[X].push_back(Y);
    }
    f.close();
}

inline bool PairUp(const int _node)
{
    if(use[_node])
        return 0;
    use[_node] = 1;
    vector<int>::iterator it;
    for(it=Graph[_node].begin();it!=Graph[_node].end();++it)
        if(!Right[*it] || PairUp(Right[*it]))
        {
            Left[_node] = *it;
            Right[*it] = _node;
            return 1;
        }
    return 0;
}
inline void Solve()
{
    int i;
    bool ok;
    do
    {
        ok = 0;
        memset(use,0,sizeof(use));
        for(i = 1;i <= N; ++i)
            if(!Left[i])
                ok += PairUp(i);
    }
    while(ok);
}

inline void Write()
{
    ofstream g(Out);
    int i,Sol = 0;
    for(i = 1; i<= N; ++i)
        if(Left[i]>0)
            Sol++;
    g<<Sol<<"\n";
    for(i = 1; i<= N; ++i)
        if(Left[i]>0)
            g<<i<<" "<<Left[i]<<"\n";
    g.close();
}

int main()
{
    Read();
    Solve();
    Write();
    return 0;
}