Cod sursa(job #1251396)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 29 octombrie 2014 13:35:38
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include <fstream>
#include <vector>
#include <cstring>

#define Nmax 10100
#define Neighbour Graph[Node][i]

using namespace std;

vector <int> Graph[Nmax];
int N, M, Solution, Left[Nmax], Right[Nmax];
bool used[Nmax];

bool hookUp(int Node) {

    int i;

    if(used[Node])
        return false;

    used[Node] = true;

    for(i = 0; i < Graph[Node].size(); i++)
        if(Left[Neighbour] == 0) {

            Right[Node] = Neighbour;
            Left[Neighbour] = Node;

            return true;

            }

    for(i = 0; i < Graph[Node].size(); i++)
        if(hookUp(Left[Neighbour])) {

            Left[Neighbour] = Node;
            Right[Node] = Neighbour;

            return true;

            }

    return false;

}
void Solve() {

    int i, last = -1;

    while(last < Solution) {

        last = Solution;
        memset(used, 0, sizeof(used));

        for(i = 1; i <= N; i++)
            if(Right[i] == 0)
                if(hookUp(i))
                    ++Solution;

        }

}
void Read() {

    int x, y, E;
    ifstream in("cuplaj.in");

    in >> N >> M >> E;

    while(E--) {

        in >> x >> y;
        Graph[x].push_back(y);

        }

    in.close();

}
void Write() {

    ofstream out("cuplaj.out");

    out << Solution << '\n';
    for(int i = 1; i <= N; i++)
        if(Right[i] != 0)
            out << i << ' ' << Right[i] << '\n';

    out.close();

}
int main() {

    Read();
    Solve();
    Write();

    return 0;
}