Cod sursa(job #1625318)

Utilizator radarobertRada Robert Gabriel radarobert Data 2 martie 2016 18:08:56
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

vector<int> g[10002];
bool vis[100002];
int l[100002], r[100002];

bool pairUp(int node)
{
    if (vis[node])
        return false;
    vis[node] = true;
    for (unsigned i = 0; i < g[node].size(); i++)
        if (r[g[node][i]] == 0 || pairUp(r[g[node][i]]))
        {
            r[g[node][i]] = node;
            l[node] = g[node][i];
            return true;
        }
    return false;
}

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

    int n, m, e;
    scanf("%d%d%d", &n, &m, &e);
    while (e--)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        g[x].push_back(y);
    }

    int sol = 0;
    bool ok = true;
    while (ok)
    {
        memset(vis, 0, sizeof(bool) * n+1);
        ok = false;
        for (int i = 1; i <= n; i++)
            if (l[i] == 0)
                if (pairUp(i))
                {
                    sol++;
                    ok = true;
                }
    }

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

    return 0;
}