Cod sursa(job #1217123)

Utilizator pulseOvidiu Giorgi pulse Data 6 august 2014 17:15:03
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

const int maxn = 100005;
int l[maxn], r[maxn], u[maxn];
vector <int> G[maxn];

int pairup(const int n)
{
    if (u[n])  return 0;
    u[n] = 1;
    for (vector <int> :: iterator it = G[n].begin(); it != G[n].end(); ++it) if (!r[*it]) {
        l[n] = *it;
        r[*it] = n;
        return 1;
    }
    for (vector <int> :: iterator it = G[n].begin(); it != G[n].end(); ++it) if (pairup(r[*it])) {
        l[n] = *it;
        r[*it] = n;
        return 1;
    }
    return 0;
}

int main()
{
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);
	int n, m, edges;
    scanf("%d %d %d", &n, &m, &edges);

    for (; edges --; )
    {
        int x, y;
        scanf("%d %d", &x, &y);
        G[x].push_back(y);
    }
    for (int change = 1; change; )
    {
        change = 0;
        memset(u, 0, sizeof(u));
        for (int i = 1; i <= n; ++i) if (!l[i])
            change |= pairup(i);
    }
    int cuplaj = 0;
    for (int i = 1; i <= n; ++i)  cuplaj += (l[i] > 0);
    printf("%d\n", cuplaj);
    for (int i = 1; i <= n; ++i) if (l[i] > 0)
        printf("%d %d\n", i, l[i]);

    return 0;
}