Cod sursa(job #1833044)

Utilizator DenisONIcBanu Denis Andrei DenisONIc Data 21 decembrie 2016 15:48:17
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>
#include <vector>
#include <cstring>
#define Nmax 10000
using namespace std;

ofstream g("cuplaj.out");


int n,m,e,x,y,nr,st[Nmax+1],dr[Nmax+1];
bool uz[Nmax+1];
vector<int> V[Nmax+1];

bool cupleaza(int nod)
{
    uz[nod] = 1;
    for (int i=0;i<V[nod].size();i++)
    {
        int crt = V[nod][i];
        if (!dr[crt])
        {
            nr++;
            st[nod] = crt;
            dr[crt] = nod;
            return 1;
        }
    }

    for (int i=0;i<V[nod].size();i++)
    {
        int crt = V[nod][i];
        if (!uz[dr[crt]] && cupleaza(dr[crt]))
        {
            st[nod] = crt;
            dr[crt] = nod;
            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++)
    {
        scanf("%d%d",&x,&y);
        V[x].push_back(y);
    }

    bool change = 1;
    while (change)
    {
        change = 0;
        memset(uz,0,sizeof(uz));
        for (int i=1;i<=n;i++)
            if (!st[i])
            {
                change |= cupleaza(i);
            }
    }

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

    return 0;
}