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

#include "bits/stdc++.h"
/*#include <iostream>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
*/
#define int long long
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define REV(i, a, n) for (int i = n; i >= a; --i)
#define all(e) e.begin(), e.end()
#define rall(e) e.rbegin(), e.rend()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define show(n) cerr << #n << " = " << n << endl
#define showp(n) cerr << n.fs << ", " << n.sc << endl
#define shows(n)                                                               \\
  for (auto z : n) {                                                           \\
    cerr << z << ", ";                                                         \\
  }                                                                            \\
  cerr << endl
#define showsp(n)                                                              \\
  for (auto z : n) {                                                           \\
    cerr << z.fs << " " << z.sc << ", "                                        \\
  }                                                                            \\
  cerr << endl

#define yes puts("Yes")
#define no puts("No")
#define case(i) printf("Case #%lld: ", i)

using namespace std;

using vi = vector<int>;
using pint = pair<int, int>;

inline void io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.tie(0);
  cout << fixed << setprecision(20);
}
inline int in() {
  int n;
  scanf("%lld", &n);
  return n;
}
inline void out(int n) { printf("%lld\\n", n); }

const int INF = 1LL << 55;
const int MOD = 1000000007;
const double EPS = 1e-8;

const int MAX_V = 1010;

int m, n;
int V;
vi G[MAX_V];
int match[MAX_V];
bool used[MAX_V];
vi blue(MAX_V / 2);
vi red(MAX_V / 2);

void add_edge(int u, int v) {
  G[u].pb(v);
  G[v].pb(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 (b == 0)
    return a;
  return gcd(b, a % b);
}

void init() {
  V = m + n;
  rep(i, MAX_V) { G[i].clear(); }
}

void solve() {
  init();
  rep(i, m) cin >> blue[i];
  rep(i, n) cin >> red[i];
  rep(i, m) {
    rep(j, n) {
      if (gcd(blue[i], red[j]) > 1) {
        add_edge(i, m + j);
      }
    }
  }
  out(bipartite_matching());
}

signed main() {
  io();

  while (cin >> m >> n, n) {
    solve();
  }

  return 0;
}


Error: -11

Fixed submission:

#include "bits/stdc++.h"
/*#include <iostream>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
*/
#define int long long
#define REP(i, a, n) for (int i = a; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define REV(i, a, n) for (int i = n; i >= a; --i)
#define all(e) e.begin(), e.end()
#define rall(e) e.rbegin(), e.rend()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define show(n) cerr << #n << " = " << n << endl
#define showp(n) cerr << n.fs << ", " << n.sc << endl
#define shows(n)                                                               \\
  for (auto z : n) {                                                           \\
    cerr << z << ", ";                                                         \\
  }                                                                            \\
  cerr << endl
#define showsp(n)                                                              \\
  for (auto z : n) {                                                           \\
    cerr << z.fs << " " << z.sc << ", "                                        \\
  }                                                                            \\
  cerr << endl

#define yes puts("Yes")
#define no puts("No")
#define case(i) printf("Case #%lld: ", i)

using namespace std;

using vi = vector<int>;
using pint = pair<int, int>;

inline void io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.tie(0);
  cout << fixed << setprecision(20);
}
inline int in() {
  int n;
  scanf("%lld", &n);
  return n;
}
inline void out(int n) { printf("%lld\\n", n); }

const int INF = 1LL << 55;
const int MOD = 1000000007;
const double EPS = 1e-8;

const int MAX_V = 1010;

int m, n;
int V;
vi G[MAX_V];
int match[MAX_V];
bool used[MAX_V];
vi blue(MAX_V / 2);
vi red(MAX_V / 2);

void add_edge(int u, int v) {
  G[u].pb(v);
  G[v].pb(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 (b == 0)
    return a;
  return gcd(b, a % b);
}

void init() {
  V = m + n;
  rep(i, MAX_V) { G[i].erase(all(G[i])); }
}

void solve() {
  init();
  rep(i, m) blue[i] = in();
  rep(i, n) red[i] = in();
  rep(i, m) {
    rep(j, n) {
      if (gcd(blue[i], red[j]) > 1) {
        add_edge(i, m + j);
      }
    }
  }
  out(bipartite_matching());
}

signed main() {
  io();

  while (cin >> m >> n, n) {
    solve();
  }

  return 0;
}