Cod sursa(job #1650366)

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