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

#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int V;
vector<int> G[1001];
int match[1001];
bool used[1001];

void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

bool dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof(used));
      if (dfs(v)) {
        res++;
      }
    }
  }
  return res;
}

int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}

int main() {
  while (true) {
    int m, n;
    cin >> m >> n;
    if (m == 0) {
      break;
    }
    vector<int> b;
    vector<int> r;
    V = m + n;
    for (int i = 0; i < 1001; i++) {
      vector<int> buf;
      G[i] = buf;
    }
    for (int i = 0; i < m; i++) {
      int buf;
      cin >> buf;
      b.push_back(buf);
    }
    for (int j = 0; j < n; j++) {
      int buf;
      cin >> buf;
      r.push_back(buf);
    }

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        if (gcd(b[i], r[j]) != 1) {
          add_edge(i, j + m);
        }
      }
    }
    cout << bipartite_matching() << endl;
  }
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int V;
vector<int> G[1001];
int match[1001];
bool used[1001];

vector<int> prime;

void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

bool dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof(used));
      if (dfs(v)) {
        res++;
      }
    }
  }
  return res;
}

void primesolve() {
  prime.push_back(2);
  prime.push_back(3);

  for (int i = 4; i < 1000000; i++) {
    bool flag = true;
    for (int k = 2; k * k <= i; k++) {
      if (i % k == 0) {
        flag = false;
        break;
      }
    }
    if (flag) {
      prime.push_back(i);
    }
  }
}
vector<int> primelist(int x) {
  set<int> s;
  int index = 0;
  while (prime[index] <= x) {
    if (x % prime[index] == 0) {
      s.insert(prime[index]);
    }
    index++;
  }
  return vector<int>(s.begin(), s.end());
}

int main() {
  primesolve();
  while (true) {
    int m, n;
    cin >> m >> n;
    if (m == 0) {
      break;
    }
    vector<int> b;
    vector<int> r;
    V = m + n;
    for (int i = 0; i < 1001; i++) {
      vector<int> buf;
      G[i] = buf;
    }
    for (int i = 0; i < m; i++) {
      int buf;
      cin >> buf;
      b.push_back(buf);
    }
    for (int j = 0; j < n; j++) {
      int buf;
      cin >> buf;
      r.push_back(buf);
    }

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        vector<int> bs = primelist(b[i]);
        vector<int> rs = primelist(r[j]);
        bool flag = true;
        for (int k = 0; k < bs.size() && flag; k++) {
          for (int l = 0; l < rs.size() && flag; l++) {
            if (bs[k] == rs[l]) {
              add_edge(i, j + m);
              flag = false;
            }
          }
        }
      }
    }
    cout << bipartite_matching() << endl;
  }
}