Cod sursa(job #1969288)

Utilizator DenisONIcBanu Denis Andrei DenisONIc Data 18 aprilie 2017 13:12:43
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <fstream>
#include <vector>
#include <cstring>
#define pb push_back
#define Nmax 10001
using namespace std;

ofstream g("cuplaj.out");

int n,m,q,st[Nmax],dr[Nmax],nr,x,y;
bool uz[Nmax],ok;
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 (dr[now]==0)
        {
            dr[now] = nod;
            st[nod] = now;
            nr++;
            return 1;
        }
    }

    for (int i=0;i<V[nod].size();i++)
    {
        int now = V[nod][i];
        if (uz[dr[now]]==0 && cup(dr[now]))
        {
            dr[now] = nod;
            st[nod] = now;
            return 1;
        }
    }
    return 0;
}

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

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

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

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

    return 0;
}