Cod sursa(job #2345073)

Utilizator Razvan85Secure Razvan Razvan85 Data 15 februarie 2019 21:18:21
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
vector <int> v[10001];
int l[10005],r[10005],n,s[10005],cmax;
int cupleaza (int x)
{
    int j;
    s[x]=1;
    for (j=0;j<v[x].size();j++)
    {
        if (!r[j])
        {
            l[x]=j;
            r[j]=x;
            return 1;
        }
    }
    for (j=0;j<v[x].size();j++)
    {
        if (!s[r[j]]&&cupleaza(r[j]))
        {
            l[x]=j;
            r[j]=x;
            return 1;
        }
    }
    return 0;
}
int m,e,x,y,i;
int main()
{
    f>>n>>m>>e;
    for (i=1;i<=e;i++)
    {
        f>>x>>y;
        v[x].push_back(y);

    }

    int ok=1,i;
    while (ok!=0)
    {
        ok=0;
        for (i=1;i<=n;i++)
        {
            s[i]=0;

        }
        for (i=1;i<=n;i++)
        {
            if (l[i]==0&&s[i]==0)
            {
                if (cupleaza(i)!=0)
                {
                    ok=1;
                    cmax++;
                }
            }
        }
    }
    g<<cmax<<'\n';
    for (i=1;i<=n;i++)
    {
        if (l[i]!=0)
        {
            g<<i<<" "<<l[i]<<'\n';
        }
    }
    return 0;
}