Cod sursa(job #1438927)

Utilizator GeorgianaMMirlogeanu Georgiana GeorgianaM Data 21 mai 2015 09:33:49
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#include <vector>
using namespace std;
#define NMAX 100001
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

bool ok=1, viz[NMAX];
int N, M,E, st[NMAX], dr[NMAX], x, y, cuplaj_max;
vector <int> G[NMAX];

bool cuplaj(int nod)
{
    if (viz[nod]) return 0;
    viz[nod]=1;
    vector <int>::iterator it=G[nod].begin();
    for (; it!=G[nod].end(); ++it)
        if (!dr[*it])
        {
            dr[*it]=nod;
            st[nod]=*it;
            return 1;
        }
    it=G[nod].begin();
    for (; it!=G[nod].end(); ++it)
        if (dr[*it] && cuplaj(dr[*it]))
        {
            dr[*it]=nod;
            st[nod]=*it;
            return 1;
        }
    return 0;
}

int main()
{
    f>>N>>M>>E;
    for (int i=1; i<=E; ++i)
        f>>x>>y,
       G[x].push_back(y);

    while (ok)
    {
        for (int i=1; i<=N; ++i)
            viz[i]=0;
        ok=0;
        for (int i=1; i<=N; ++i)
            if (!st[i] && cuplaj(i))
                ok=1, ++cuplaj_max, cuplaj(i);
    }

    g<<cuplaj_max<<'\n';
    for (int i=1; i<=N; ++i)
        if (st[i]) g<<i<<' '<<st[i]<<'\n';
    return 0;
}