Cod sursa(job #1413335)

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

int N, M, nrm, x, y, cuplate, st[100005], dr[100005], ok=1;
vector < int > G[100005];
bitset < 100005 > viz;

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<=nrm; ++i)
        f>>x>>y, G[x].push_back(y);

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

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