Cod sursa(job #902307)

Utilizator Catah15Catalin Haidau Catah15 Data 1 martie 2013 13:36:05
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <iostream>
#include <cstdio>
#include <cstring>
#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)
    {
        int nod2 = List[nod][i];

        if (! ST[nod2] || Cupleaza (ST[nod2]))
        {
            ST[nod2] = nod;
            DR[nod] = nod2;
            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 u, v;
        scanf ("%d %d", &u, &v);

        List[u].PB (v);
    }

    int sol = 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)) ++ sol, ok = true;
    }


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

    return 0;
}