Cod sursa(job #1192931)

Utilizator andreiiiiPopa Andrei andreiiii Data 30 mai 2014 11:27:41
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <vector>

using namespace std;

const int Nmax = 10005;

vector<int> G[Nmax];
bitset<Nmax> v;
int L[Nmax], R[Nmax];

int N, M;

int PairUp(const int node)
{
    if (v[node]) return 0;
    v[node] = 1;

    for (vector<int>::iterator it = G[node].begin(); it != G[node].end(); it++)
    {
        if (!R[*it])
        {
            R[*it] = node;
            L[node] = *it;
            return 1;
        }
    }

    for (vector<int>::iterator it = G[node].begin(); it != G[node].end(); it++)
    {
        if (PairUp(R[*it]))
        {
            R[*it] = node;
            L[node] = *it;
            return 1;
        }
    }

    return 0;
}

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

    int Q;
    scanf("%d%d%d", &N, &M, &Q);

    while (Q--)
    {
        int x, y;
        scanf("%d%d", &x, &y);

        G[x].push_back(y);
    }

    for (int ok = 1; ok; )
    {
        ok = 0;

        v.reset();
        for (int i = 1; i <= N; i++)
            if (!L[i])
                ok |= PairUp(i);
    }

    int Sol = 0;
    for (int i = 1; i <= N; i++)
        if (L[i])
            Sol++;

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