Cod sursa(job #1784654)

Utilizator team_nameUPB Banu Popa Visan team_name Data 20 octombrie 2016 12:28:55
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;

const int nmax = 10010;

int n, m, e, x, y, l[nmax], r[nmax];
bool used[nmax];
vector<int> g[nmax];

bool pairup(int node)
{
    if (used[node]) return 0;
    used[node] = 1;

    for (int vec : g[node])
        if (!r[vec])
        {
            l[node] = vec;
            r[vec] = node;
            return 1;
        }
    for (int vec : g[node])
        if (pairup(r[vec]))
        {
            l[node] = vec;
            r[vec] = node;
            return 1;
        }
    return 0;
}

int get_matching()
{
    int ans = 0, ok = 1;
    while (ok)
    {
        ok = 0;
        for (int i = 1; i <= n; ++ i) used[i] = 0;
        for (int i = 1; i <= n; ++ i)
            if (!l[i] && pairup(i))
                ok = 1, ans ++;
    }
    return ans;
}

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

    //freopen("in", "r", stdin);

    scanf("%i %i %i", &n, &m, &e);
    for (int i = 1; i <= e; ++ i)
    {
        scanf("%i %i", &x, &y);
        g[x].push_back(y);
    }

    printf("%i\n", get_matching());
    for (int i = 1; i <= n; ++ i)
        if (l[i])
            printf("%i %i\n", i, l[i]);
}