Problem p00744 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

#define vec vector
#define pb push_back
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
// typedef long long Int;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst) : src(src), dst(dst) {}
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight
         : e.src != f.src     ? e.src < f.src
                              : e.dst < f.dst;
}
typedef vec<Edge> Edges;
typedef vec<Edges> Graph;

#define MAXMN 500
int B[MAXMN];
int R[MAXMN];

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }

bool augment(const Graph &g, int u, vector<int> &matchTo,
             vector<bool> &visited) {
  if (u < 0)
    return true;
  FOR(e, g[u]) if (!visited[e->dst]) {
    visited[e->dst] = true;
    if (augment(g, matchTo[e->dst], matchTo, visited)) {
      matchTo[e->src] = e->dst;
      matchTo[e->dst] = e->src;
      return true;
    }
  }
  return false;
}
int bipartiteMatching(const Graph &g, int L) {
  const int n = g.size();
  vector<int> matchTo(n, -1);
  int match = 0;
  REP(u, L) {
    vector<bool> visited(n);
    if (augment(g, u, matchTo, visited))
      ++match;
  }
  return match;
}

int main(int argc, char **argv) {
  int M, N;
  Graph g;
  g.reserve(MAXMN + MAXMN);
  while (cin >> M >> N, M) {
    g.clear();
    g.resize(M + N);
    REP(i, M) cin >> B[i];
    REP(i, N) cin >> R[i];
    REP(i, M) {
      Edges es, es2;
      REP(j, N) {
        if (gcd(B[i], R[j]) != 1) {
          es.pb(Edge(i, M + j));
          es2.pb(Edge(M + j, i));
        }
      }
      g[i] = es;
      g[M + i] = es2;
    }
    cout << bipartiteMatching(g, M) << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

#define vec vector
#define pb push_back
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
// typedef long long Int;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst) : src(src), dst(dst) {}
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight
         : e.src != f.src     ? e.src < f.src
                              : e.dst < f.dst;
}
typedef vec<Edge> Edges;
typedef vec<Edges> Graph;

#define MAXMN 500
int B[MAXMN];
int R[MAXMN];

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }

bool augment(const Graph &g, int u, vector<int> &matchTo,
             vector<bool> &visited) {
  if (u < 0)
    return true;
  FOR(e, g[u]) if (!visited[e->dst]) {
    visited[e->dst] = true;
    if (augment(g, matchTo[e->dst], matchTo, visited)) {
      matchTo[e->src] = e->dst;
      matchTo[e->dst] = e->src;
      return true;
    }
  }
  return false;
}
int bipartiteMatching(const Graph &g, int L) {
  const int n = g.size();
  vector<int> matchTo(n, -1);
  int match = 0;
  REP(u, L) {
    vector<bool> visited(n);
    if (augment(g, u, matchTo, visited))
      ++match;
  }
  return match;
}

int main(int argc, char **argv) {
  int M, N;
  Graph g;
  g.reserve(MAXMN + MAXMN);
  while (cin >> M >> N, M) {
    g.clear();
    g.resize((M + N) * 2);
    REP(i, M) cin >> B[i];
    REP(i, N) cin >> R[i];
    REP(i, M) {
      Edges es, es2;
      REP(j, N) {
        if (gcd(B[i], R[j]) != 1) {
          es.pb(Edge(i, M + j));
          es2.pb(Edge(M + j, i));
        }
      }
      g[i] = es;
      g[M + i] = es2;
    }
    cout << bipartiteMatching(g, M) << endl;
  }
  return 0;
}