Cod sursa(job #1112934)

Utilizator rares96cheseliRares Cheseli rares96cheseli Data 20 februarie 2014 10:24:18
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

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

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

    for (vector <int>::iterator it=G[nod].begin(); 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)
    {
        ok=0;
        for (int i=1; i<=N; ++i)
            viz[i]=0;
        for (int i=1; i<=N; ++i)
            if (!st[i] && cuplaj(i))
                ++sol, cuplaj(i), ok=1;
    }
    g<<sol<<'\n';
    for (int i=1; i<=N; ++i)
        if (st[i]) g<<i<<' '<<st[i]<<'\n';
    return 0;
}