Cod sursa(job #1247954)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 24 octombrie 2014 13:04:15
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
#define Nmax 10002
#define Mmax 10002

int n, C = 0;
vector< int > G[Nmax];//, st(Nmax), dr(Mmax);
int st[Nmax], dr[Mmax];
vector< bool > viz(Nmax);

bool pair_up(int) ;
bool ok() ;

int main()
{
    int m, e, a, b;
    fin >> n >> m >> e;
    
    for(; e; --e)
    {
        fin >> a >> b;
        G[a].push_back(b);
    }
    
    while( ok() )
        a = 1;
    
    fout << C << '\n';
    
    for(a = 1; a <= n; ++a)
        if(dr[a]) fout << a << ' ' << dr[a] << '\n';
    return 0;
}


bool pair_up(int vf)
{
    if(viz[vf]) return 0;
    viz[vf] = 1;
    
    for(auto it = G[vf].begin(); it != G[vf].end(); ++it)
        if(st[*it] == 0)
        {
            st[*it] = vf;
            dr[vf] = *it;
            return 1;
        }
    
    for(auto it = G[vf].begin(); it != G[vf].end(); ++it)
        if(pair_up(st[*it]))
        {
            st[*it] = vf;
            dr[vf] = *it;
            return 1;
        }
    return 0;
}


bool ok()
{
    bool rez = 0;
    fill(viz.begin(), viz.end(), 0);
    
    for(int i = 1; i <= n; ++i)
        if(!dr[i] && pair_up(i)) rez = 1, ++C;
    return rez;
}