Cod sursa(job #2419983)

Utilizator alexoloieriAlexandru Oloieri alexoloieri Data 9 mai 2019 23:47:00
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <bits/stdc++.h>
#define nmax 10005
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
using namespace std;
int n, m, e, x, y;
vector<int>g[nmax];
int st[nmax], dr[nmax];
bool viz[nmax];
vector<pii>ans;
bool cup(int nod)
{
    if (viz[nod]) return false;
    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 (!viz[i] && !st[i])
                if (cup(i))
                    ok = true;
    }
    for (int i=1;i<=n;++i)
        if (st[i])
           ans.push_back(mp(i,st[i]));
    printf("%d\n",ans.size());
    for (auto pr:ans)
        printf("%d %d\n",pr.first,pr.second);
    fclose(stdin);
    fclose(stdout);
    return 0;
}