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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))

struct edge {
  int to, co, p;
};

vector<edge> vs[1005];

void cleae() { rep(i, 1005) vs[i].clear(); }

void adde(int a, int b, int c) {
  // printf("fr %d to %d co %d\\n",a,b,c);
  int sa = vs[a].size(), sb = vs[b].size();
  edge e;
  e.co = c;
  e.to = b;
  e.p = sb;
  vs[a].push_back(e);
  e.co = 0;
  e.to = a;
  e.p = sa;
  vs[b].push_back(e);
}

int st, gl;
int gone[1005];
int dfs(int no, int f) {
  if (gone[no])
    return 0;
  gone[no] = 1;
  if (no == gl)
    return f;
  rep(i, vs[no].size()) {
    edge &e = vs[no][i];
    if (gone[e.to])
      continue;
    if (e.co > 0) {
      int gf = dfs(e.to, min(e.co, f));
      if (gf > 0) {
        e.co -= gf;
        vs[e.to][e.p].co += gf;
        return gf;
      }
    }
  }
  return 0;
}

int flow() {
  int res = 0;
  for (;;) {
    memset(gone, 0, sizeof(gone));
    int f = dfs(st, IINF);
    if (f == 0)
      return res;
    res += f;
  }
}

int na, nb;
int da[505];
int db[505];
int gcd(int p, int q) {
  if (p < q)
    swap(p, q);
  if (p % q == 0)
    return q;
  return gcd(q, p % q);
}

int main(void) {
  for (;;) {
    scanf("%d%d", &na, &nb);
    if (na == 0)
      break;
    rep(i, na) scanf("%d", &da[i]);
    rep(i, nb) scanf("%d", &db[i]);
    cleae();

    st = 0;
    gl = na + nb + 1;
    rep(i, na) adde(st, i + 1, 1);
    rep(i, nb) adde(i + na + 1, gl, 1);

    rep(i, na) rep(j, nb) {
      if (gcd(da[i], db[j]) > 1)
        adde(i + 1, j + na + 1, 1);
    }
    printf("%d\\n", flow());
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))

struct edge {
  int to, co, p;
};

vector<edge> vs[505];

void cleae() { rep(i, 505) vs[i].clear(); }

void adde(int a, int b, int c) {
  // printf("fr %d to %d co %d\\n",a,b,c);
  int sa = vs[a].size(), sb = vs[b].size();
  edge e;
  e.co = c;
  e.to = b;
  e.p = sb;
  vs[a].push_back(e);
  e.to = a;
  e.p = sa;
  vs[b].push_back(e);
}

int st, gl;
int gone[505];
int dfs(int no, int f) {
  if (gone[no])
    return 0;
  gone[no] = 1;
  if (no == gl)
    return f;
  rep(i, vs[no].size()) {
    edge &e = vs[no][i];
    if (gone[e.to])
      continue;
    if (e.co > 0) {
      int gf = dfs(e.to, min(e.co, f));
      if (gf > 0) {
        e.co -= gf;
        vs[e.to][e.p].co += gf;
        return gf;
      }
    }
  }
  return 0;
}

int flow() {
  int res = 0;
  for (;;) {
    memset(gone, 0, sizeof(gone));
    int f = dfs(st, IINF);
    if (f == 0)
      return res;
    res += f;
  }
}

int na, nb;
int da[505];
int db[505];
int gcd(int p, int q) {
  if (p < q)
    swap(p, q);
  if (p % q == 0)
    return q;
  return gcd(q, p % q);
}

int main(void) {
  for (;;) {
    scanf("%d%d", &na, &nb);
    if (na == 0)
      break;
    rep(i, na) scanf("%d", &da[i]);
    rep(i, nb) scanf("%d", &db[i]);
    cleae();

    st = 0;
    gl = na + nb + 1;
    rep(i, na) adde(st, i + 1, 1);
    rep(i, nb) adde(i + na + 1, gl, 1);

    rep(i, na) rep(j, nb) {
      if (gcd(da[i], db[j]) > 1)
        adde(i + 1, j + na + 1, 1);
    }
    printf("%d\\n", flow());
  }
  return 0;
}