Cod sursa(job #1116327)

Utilizator rares96cheseliRares Cheseli rares96cheseli Data 22 februarie 2014 14:46:09
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

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

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>>nrm;
    for (int i=1; i<=N; ++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, ++sol, cuplaj(i);
    }

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