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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (long long)(1e18)
#define MOD (int)(1e9 + 7)

#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")
#define MAX 1100
#define MAX_V MAX

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

vector<edge> G[MAX_V];
bool used[MAX_V];

int V, E;
int n, m;
int a[MAX], b;

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

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    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;
  for (;;) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

signed main() {
  cout << fixed << setprecision(7);

  while (true) {

    cin >> m >> n;

    if (!n && !m)
      break;

    for (int i = 0; i <= n + m + 1; i++)
      G[i].clear();

    for (int i = 1; i <= m; i++) {
      cin >> a[i];
      add_edge(0, i, 1);
    }

    for (int i = 1; i <= n; i++) {
      cin >> b;
      for (int j = 1; j <= m; j++) {
        if (__gcd(a[j], b) != 1) {
          add_edge(j, i + m, 1);
        }
      }
      add_edge(i + m, n + m + 1, 1);
    }

    cout << max_flow(0, n + m + 1) << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (long long)(1e18)
#define MOD (int)(1e9 + 7)

#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")
#define MAX 300
#define MAX_V MAX

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

vector<edge> G[MAX_V];
bool used[MAX_V];

int V, E;
int n, m;
int a[MAX], b;

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

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    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;
  for (;;) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

signed main() {
  cout << fixed << setprecision(7);

  while (true) {

    cin >> m >> n;

    if (!n && !m)
      break;

    for (int i = 0; i <= n + m + 1; i++)
      G[i].clear();

    for (int i = 1; i <= m; i++) {
      cin >> a[i];
      add_edge(0, i, 1);
    }

    for (int i = 1; i <= n; i++) {
      cin >> b;
      for (int j = 1; j <= m; j++) {
        if (__gcd(a[j], b) != 1) {
          add_edge(j, i + m, 1);
        }
      }
      add_edge(i + m, n + m + 1, 1);
    }

    cout << max_flow(0, n + m + 1) << endl;
  }

  return 0;
}