Cod sursa(job #1540606)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 2 decembrie 2015 22:56:41
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
#include <cstdio>
#include <bitset>
#include <vector>

using namespace std;

const int DIM = 65536;

int answer, nrNodes1, nrNodes2, nrEdges, node1, node2, ok;
vector <int> Edges[DIM]; bitset <DIM> Marked; int Left[DIM], Right[DIM];

inline bool vertexCover (int node) {
    vector <int> :: iterator it;

    if (Marked[node])
        return 0;
    Marked[node] = 1;

    for (it = Edges[node].begin(); it != Edges[node].end(); it ++) {
        int nextNode = *it;

        if (!Right[nextNode]) {
            Left[node] = nextNode;
            Right[nextNode] = node;
            answer ++;

            return 1;
        }
    }

    for (it = Edges[node].begin(); it != Edges[node].end(); it ++) {
        int nextNode = *it;

        if (vertexCover(Right[nextNode])) {
            Left[node] = nextNode;
            Right[nextNode] = node;

            return 1;
        }
    }

    return 0;
}

int main () {

    freopen ("cuplaj.in" ,"r", stdin );
    freopen ("cuplaj.out","w", stdout);

    scanf ("%d", &nrNodes1);
    scanf ("%d", &nrNodes2);
    scanf ("%d", &nrEdges );

    for (int i = 1; i <= nrEdges; i ++) {
        scanf ("%d %d", &node1, &node2);
        Edges[node1].push_back(node2);
    }

    do {
        ok = 1;
        Marked.reset();

        for (int i = 1; i <= nrNodes1; i ++)
            if (!Left[i] && vertexCover(i))
                ok = 0;

    } while (!ok);

    printf ("%d\n", answer);
    for (int i = 1; i <= nrNodes1; i ++)
        if (Left[i]) printf ("%d %d\n", i, Left[i]);

    return 0;
}