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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
#define SET(a, c) memset(a, c, sizeof(a))

#define DEBUG(x)                                                               \\
  cout << "#x"                                                                 \\
       << ": " << x << endl

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = INT_MAX / 3;
const ll MOD = 1000000007;
const double EPS = 1e-14;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

struct edge {
  int to;
  int cap;
  int rev;
};
int m, n;

vector<edge> G[2012];
bool used[2012];
int a[511], b[511];

void add_edge(int from, int to) {
  G[from].PB((edge){to, 1, (int)G[to].size()});
  G[to].PB((edge){from, 0, (int)G[from].size() - 1});
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  REP(i, G[v].size()) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (true) {
    REP(i, m + n + 2) used[i] = false;
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
  return -1;
}

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

int main() {

  while (true) {
    m, n;
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;
    REP(i, n + m + 2) { G[i].clear(); }
    REP(i, n + m + 2) used[i] = false;
    REP(i, m) {
      cin >> a[i];
      add_edge(0, i + 1);
    }
    REP(i, n) {
      cin >> b[i];
      add_edge(i + 1 + m, n + m + 1);
    }
    REP(i, m) {
      REP(j, n) {
        if (gcd(a[i], b[j]) > 1) {
          add_edge(i + 1, m + j + 1);
        }
      }
    }
    cout << max_flow(0, n + m + 1) << endl;
    ;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))
#define SET(a, c) memset(a, c, sizeof(a))

#define DEBUG(x)                                                               \\
  cout << "#x"                                                                 \\
       << ": " << x << endl

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = INT_MAX / 3;
const ll MOD = 1000000007;
const double EPS = 1e-14;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

struct edge {
  int to;
  int cap;
  int rev;
};
int m, n;

vector<edge> G[1002];
bool used[1002];
int a[501], b[501];

void add_edge(int from, int to) {
  G[from].PB((edge){to, 1, (int)G[to].size()});
  G[to].PB((edge){from, 0, (int)G[to].size() - 1});
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  REP(i, G[v].size()) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (true) {
    SET(used, false);
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
  return -1;
}

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

int main() {

  while (true) {
    m, n;
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;
    REP(i, 1002) G[i].clear();
    SET(used, false);
    REP(i, m) {
      cin >> a[i];
      add_edge(0, i + 1);
    }
    REP(i, n) {
      cin >> b[i];
      add_edge(i + 1 + m, n + m + 1);
    }
    REP(i, m) {
      REP(j, n) {
        if (gcd(a[i], b[j]) > 1) {
          add_edge(i + 1, m + j + 1);
        }
      }
    }
    cout << max_flow(0, n + m + 1) << endl;
    ;
  }

  return 0;
}