Cod sursa(job #862226)

Utilizator deneoAdrian Craciun deneo Data 22 ianuarie 2013 14:12:07
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

#define MAXN 11000

int N, M, E, cuplaj[MAXN], used[MAXN], pereche[MAXN], rez;
vector<int> graph[MAXN];

bool pairup(int nod) {
    if (used[nod]) return 0;
    used[nod] = 1;

    for (int i = 0; i < graph[nod].size(); ++i) {
        int node = graph[nod][i];

        if (!pereche[node] || pairup(pereche[node])) {
            cuplaj[nod] = node;
            pereche[node] = nod;
            return 1;
        }
    }
    return 0;
}


int main()  {
    fin >> N >> M >> E;

    for (int i = 1; i <= E; ++i) {
        int x, y;
        fin >> x >> y;
        graph[x].push_back(y);
    }

    int ok = 1;

    while (ok) {
        ok = 0;
        memset(used, 0, sizeof(used));
        for (int i = 1; i <= N; ++i) {
            if (!cuplaj[i])
                if(pairup(i)) {
                    ++rez;
                    ok = 1;
                }
        }
    }

    fout << rez << "\n";

    for (int i = 1; i <= N; ++i)
        if (cuplaj[i])
            fout << i << " " << cuplaj[i] << "\n";

    return 0;
}