Cod sursa(job #1883589)

Utilizator vladdy47Bucur Vlad Andrei vladdy47 Data 18 februarie 2017 09:26:24
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
# include <bits/stdc++.h>

using namespace std;

const int Nmax = 10000 + 5;

int N, M, E, i, OK, x, y, ap[Nmax], Left[Nmax], Right[Nmax];

vector <int> G[Nmax];

int cuplaj(int x)
{
    if (ap[x]) return 0;

    ap[x] = 1;

    for (auto &it: G[x])
        if (!Right[it])
        {
            Left[x] = it, Right[it] = x;
            return 1;
        }

    for (auto &it: G[x])
    {
        if (cuplaj(Right[it]))
        {
            Left[x] = it, Right[it] = x;
            return 1;
        }
        return 0;
    }

    return 0;
}


int main ()
{
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);

    scanf("%d %d %d\n", &N, &M, &E);

    for (i = 1; i <= E; ++i)
    {
        scanf("%d %d", &x, &y);
        G[x].push_back(y);
    }

    OK = 1;

    while (OK)
    {
        memset(ap, 0, sizeof(ap)), OK = 0;

        for (i = 1; i <= N; ++i)
            if (!Left[i]) OK += cuplaj(i);
    }

    for (i = 1; i <= N; ++i)
       if (Left[i]) printf("%d %d\n", i, Left[i]);

    return 0;
}