Cod sursa(job #2419131)

Utilizator alexoloieriAlexandru Oloieri alexoloieri Data 7 mai 2019 18:20:39
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <bits/stdc++.h>
#define nmax 10005
#define pb push_back
using namespace std;
int n, m, e, x, y;
vector<int>g[nmax];
int st[nmax], dr[nmax], ans;
bool viz[nmax];
bool cup(int nod)
{
    if (viz[nod]) return 0;
    viz[nod] = true;
    for (auto x:g[nod])
    {
        if (!dr[x])
        {
            st[nod] = x;
            dr[x] = nod;
            return true;
        }
    }
    for (auto x:g[nod])
    {
        if (cup(dr[x]))
        {
            st[nod] = x;
            dr[x] = nod;
            return true;
        }
    }
    return false;
}
int main()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    scanf("%d %d %d",&n,&m,&e);
    for (int i=1; i<=e; ++i)
    {
        scanf("%d %d",&x,&y);
        g[x].pb(y);
    }
    bool ok = true;
    while(ok)
    {
        ok = false;
        memset(viz,0,sizeof(viz));
        for (int i=1; i<=n; ++i)
            if (!st[i] && !viz[i])
            {
                if (cup(i))
                    ok = true;
            }
    }
    for (int i=1; i<=n; ++i)
        if (st[i])
            ++ans;
    printf("%d\n",ans);
    for (int i=1; i<=n; ++i)
        if (st[i])
            printf("%d %d\n",i,st[i]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}