Cod sursa(job #1898227)

Utilizator DenisONIcBanu Denis Andrei DenisONIc Data 1 martie 2017 21:32:58
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
#include <cstdio>
#include <vector>
#include <cstring>
#define Nmax 10001
using namespace std;

ofstream g("cuplaj.out");

int n,m,e,S[Nmax],D[Nmax],nr;
bool uz[Nmax];
vector<int> V[Nmax];

bool cup(int nod)
{
    uz[nod] = 1;
    for (int i=0;i<V[nod].size();i++)
    {
        int now = V[nod][i];
        if (!D[now])
        {
            D[now] = nod;
            S[nod] = now;
            nr++;
            return 1;
        }
    }
    for (int i=0;i<V[nod].size();i++)
    {
        int now = V[nod][i];
        if (!uz[D[now]] && cup(D[now]))
        {
            D[now] = nod;
            S[nod] = now;
            return 1;
        }
    }
    return 0;
}

int main()
{
    freopen("cuplaj.in","r",stdin);
    scanf("%d%d%d",&n,&m,&e);

    for (int i=1;i<=e;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        V[x].push_back(y);
    }


    bool ok = 1;
    while (ok)
    {
        ok = 0;
        memset(uz,0,sizeof(uz));
        for (int i=1;i<=n;i++)
        {
            if (!uz[i] && !S[i])
                ok |= cup(i);
        }
    }

    g<<nr<<'\n';
    for (int i=1;i<=n;i++)
    {
        if (S[i]!=0)
            g<<i<<' '<<S[i]<<'\n';
    }

    return 0;
}