Cod sursa(job #2739454)

Utilizator stefantagaTaga Stefan stefantaga Data 8 aprilie 2021 12:17:13
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
vector <int> v[10005];
int st[10005],dr[10005],sel[10005],e,n,m,i,x,y,ok;
bool cuplaj(int x)
{
    int i;
    sel[x]=1;
    for (i=0;i<v[x].size();i++)
    {
        if (dr[v[x][i]]==0)
        {
            st[x]=v[x][i];
            dr[v[x][i]]=x;
            return 1;
        }
    }
    for (i=0;i<v[x].size();i++)
    {
        if (sel[dr[v[x][i]]]==0&&cuplaj(dr[v[x][i]])==1)
        {
            st[x]=v[x][i];
            dr[v[x][i]]=x;
            return 1;
        }
    }
    return 0;
}
int main()
{
    f>>n>>m>>e;
    for (i=1;i<=e;i++)
    {
        f>>x>>y;
        v[x].push_back(y);
    }
    ok=1;
    while (ok==1)
    {
        ok=0;
        for (i=1;i<=n;i++)
        {
            sel[i]=0;
        }
        for (i=1;i<=n;i++)
        {
            if (st[i]==0&&sel[i]==0)
            {
                ok=ok|cuplaj(i);
            }
        }
    }
    int sum=0;
    for (i=1;i<=n;i++)
    {
        if (st[i]!=0)
        {
            sum++;
        }
    }
    g<<sum<<'\n';
    for (i=1;i<=n;i++)
    {
        if (st[i]!=0)
        {
            g<<i<<" "<<st[i]<<'\n';
        }
    }
    return 0;
}