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

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define MP make_pair
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STRING(x)                                                              \\
  string x;                                                                    \\
  cin >> x;
#define STR(x) STRING(x);
#define DOUBLE(x)                                                              \\
  double x;                                                                    \\
  cin >> x;
#define DBL(x) DOUBLE(x);
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl
#define INIT(a) memset(a, -1, sizeof a)
#define CLR(a) memset(a, 0, sizeof a)
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
// const int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
// const int M = 1000000007;
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

struct edge {
  int to, cap, rev;
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};
vector<edge> G[1200];
bool used[1200];
void add_edge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  FOR(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) {
    CLR(used);
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}
int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  while (true) {
    int m, n;
    cin >> m >> n;
    if (!m && !n)
      break;
    int s = m + n, t = s + 1;
    vi b, r;
    FOR(i, 1200) G[i].clear();
    FOR(i, m) {
      INT(x);
      b.push_back(x);
      add_edge(s, i, 1);
    }

    FOR(i, n) {
      INT(x);
      r.push_back(x);
      add_edge(m + i, t, 1);
    }

    FOR(i, m) {
      FOR(j, n) {
        if (gcd(b[i], r[j]) != 1) {
          add_edge(i, j + m, 1);
        }
      }
    }
    cout << max_flow(s, t) << endl;
  }
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define MP make_pair
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STRING(x)                                                              \\
  string x;                                                                    \\
  cin >> x;
#define STR(x) STRING(x);
#define DOUBLE(x)                                                              \\
  double x;                                                                    \\
  cin >> x;
#define DBL(x) DOUBLE(x);
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl
#define INIT(a) memset(a, -1, sizeof a)
#define CLR(a) memset(a, 0, sizeof a)
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
// const int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
// const int M = 1000000007;
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

struct edge {
  int to, cap, rev;
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};
vector<edge> G[600];
bool used[600];
void add_edge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  FOR(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) {
    CLR(used);
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}
int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  while (true) {
    int m, n;
    cin >> m >> n;
    if (!m && !n)
      break;
    int s = m + n, t = s + 1;
    vi b, r;
    vector<edge> Empty[600];
    swap(Empty, G);
    FOR(i, m) {
      INT(x);
      b.push_back(x);
      add_edge(s, i, 1);
    }

    FOR(i, n) {
      INT(x);
      r.push_back(x);
      add_edge(m + i, t, 1);
    }

    FOR(i, m) {
      FOR(j, n) {
        if (gcd(b[i], r[j]) != 1) {
          add_edge(i, j + m, 1);
        }
      }
    }
    cout << max_flow(s, t) << endl;
  }
}