Problem p00744 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

typedef int Int;

// math
Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; }
Int lcm(Int a, Int b) { return a * b / gcd(a, b); }
// a x + b y = gcd(a, b)
Int extgcd(Int a, Int b, Int &x, Int &y) {
  Int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

// Graph
typedef Int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  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 : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

// bipartite matching
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, Edges &matching) {
  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;
  }
  REP(u, L)
  if (matchTo[u] >= 0) // make explicit matching
      matching.push_back(Edge(u, matchTo[u], 0));
  return match;
}

int main() {
  int m, n;
  while (cin >> m >> n) {
    if (m == 0 && n == 0)
      break;

    int B[501];
    int R[501];
    for (int i = 0; i < m; i++)
      cin >> B[i];
    for (int i = 0; i < n; i++)
      cin >> R[i];

    Graph g(m + n);
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        if (gcd(B[i], R[j]) > 1) {
          g[i].push_back(Edge(i, m + j, 0));
        }
      }
    }

    Edges edges;
    cout << bipartiteMatching(g, m, edges) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

typedef int Int;

// math
Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; }
Int lcm(Int a, Int b) { return a * b / gcd(a, b); }
// a x + b y = gcd(a, b)
Int extgcd(Int a, Int b, Int &x, Int &y) {
  Int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

// Graph
typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  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 : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

// bipartite matching
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, Edges &matching) {
  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;
  }
  REP(u, L)
  if (matchTo[u] >= 0) // make explicit matching
      matching.push_back(Edge(u, matchTo[u], 0));
  return match;
}

int main() {
  int m, n;
  while (cin >> m >> n) {
    if (m == 0 && n == 0)
      break;
    int B[501];
    int R[501];
    for (int i = 0; i < m; i++)
      cin >> B[i];
    for (int i = 0; i < n; i++)
      cin >> R[i];

    Graph g(2 * m);
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        if (gcd(B[i], R[j]) > 1) {
          g[i].push_back(Edge(i, m + j, 1));
        }
      }
    }

    Edges edges;
    cout << bipartiteMatching(g, m, edges) << endl;
  }
  return 0;
}