Cod sursa(job #1333847)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 3 februarie 2015 17:02:57
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include <fstream>
#include <vector>
#include <cstring>

#define Nmax 10100
#define neighbour Graph[node][i]

using namespace std;

vector <int> Graph[Nmax];
int couples, N, M, Left[Nmax], Right[Nmax];
bool seen[Nmax];

bool hookup(int node) {

    if(seen[node])
        return false;
    else
        seen[node] = true;

    for(int i = 0; i < Graph[node].size(); i++)
        if(Left[neighbour] == 0) {
            Right[node] = neighbour;
            Left[neighbour] = node;
            return true;
        }

    for(int i = 0; i < Graph[node].size(); i++)
        if(hookup(Left[neighbour])) {
            Right[node] = neighbour;
            Left[neighbour] = node;
            return true;
        }

    return false;
}
void Solve() {

    int i, last;

    last = -1;

    while(last < couples) {

        last = couples;
        memset(seen, 0, sizeof(seen));

        for(i = 1; i <= N; i++)
            if(Right[i] == 0 && hookup(i))
                couples++;
    }

}
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 << couples << '\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;
}