Cod sursa(job #874419)

Utilizator Catah15Catalin Haidau Catah15 Data 8 februarie 2013 13:30:32
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

#define maxN 10005
#define PB push_back

int DR[maxN], ST[maxN];
bool Viz[maxN];
vector <int> List[maxN];


bool cupleaza (int nod)
{
    if (Viz[nod]) return false;
    Viz[nod] = true;

    for (int i = 0; i < List[nod].size(); ++ i)
        if (! ST[List[nod][i]] || cupleaza (ST[List[nod][i]]))
        {
            ST[List[nod][i]] = nod;
            DR[nod] = List[nod][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);

        List[x].PB (y);
    }

    int cuplaj = 0;
    bool ok = true;

    while (ok)
    {
        ok = false;
        for (int i = 1; i <= N; ++ i) Viz[i] = false;

        for (int i = 1; i <= N; ++ i)
            if (! DR[i] && cupleaza (i)) ++ cuplaj, ok = true;

    }

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

    return 0;
}