Cod sursa(job #1374969)

Utilizator vlady1997Vlad Bucur vlady1997 Data 5 martie 2015 11:35:17
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
vector <int> g[10001];
int st[10001], dr[10001], n1, n2;
bool used[10001];
bool cuplaj (int x)
{
    if (used[x]==true) return false;
    int i, y; used[x]=true;
    for (i=0; i<g[x].size(); i++)
    {
        y=g[x][i];
        if (dr[y]==0)
        {
            st[x]=y;
            dr[y]=x;
            return true;
        }
        if (cuplaj(dr[y]))
        {
            st[x]=y;
            dr[y]=x;
            return true;
        }
    }
    return false;
}
int main()
{
    int m, i, x, y, nr=0;
    bool ok=true;
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    scanf("%d%d%d",&n1,&n2,&m);
    for (i=1; i<=m; i++)
    {
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
    }
    while (ok==true)
    {
        memset(used,0,sizeof(used)); ok=false;
        for (i=1; i<=n1; i++)
        {
            if (st[i]==0)
            {
                if (cuplaj(i))
                {
                    ok=true;
                }
            }
        }
    }
    for (i=1; i<=n1; i++)
    {
        if (st[i]!=0) nr++;
    }
    printf("%d\n",nr);
    for (i=1; i<=n1; i++)
    {
        if (st[i]!=0) printf("%d %d\n",i,st[i]);
    }
    return 0;
}