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

#include <algorithm>
#include <assert.h>
#include <functional>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

#define inf 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)

typedef long long ll;

class path {
public:
  int t;
  ll c;
  int p;
};

class dinic {
public:
  int n;
  vector<path> p;
  vector<int> *v;
  int *l;
  int *o;
  dinic(int nw) {
    n = nw;
    v = new vector<int>[n];
    l = new int[n];
    o = new int[n];
  }
  void add(int from, int to, ll cap) {
    assert(from < n);
    assert(to < n);
    path w1, w2;
    w1.t = to;
    w1.c = cap;
    w1.p = p.size() + 1;
    v[from].push_back(p.size());
    w2.t = from;
    w2.c = 0;
    w2.p = p.size();
    v[to].push_back(p.size() + 1);
    p.push_back(w1);
    p.push_back(w2);
  }
  void dics(int from) {
    rep(i, n) { l[i] = inf; }
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        pq;
    pq.push(make_pair(0, from));
    while (!pq.empty()) {
      int k = pq.top().first;
      int m = pq.top().second;
      pq.pop();
      if (l[m] > k) {
        l[m] = k;
        rep(i, v[m].size()) {
          int x = v[m][i];
          if (p[x].c > 0) {
            pq.push(make_pair(k + 1, p[x].t));
          }
        }
      }
    }
  }
  ll dfs(int now, int to, ll mi) {
    if (now == to) {
      return mi;
    }
    ll all = 0;
    while (o[now] < v[now].size()) {
      int x = v[now][o[now]];
      if (l[p[x].t] == l[now] + 1) {
        ll w = dfs(p[x].t, to, min(mi - all, p[x].c));
        p[x].c -= w;
        p[p[x].p].c += w;
        all += w;
        if (mi - all == 0) {
          break;
        }
      }
      o[now]++;
    }
    return all;
  }
  ll solve(int from, int to) {
    assert(from < n);
    assert(to < n);
    ll all = 0;
    while (1) {
      dics(from);
      rep(i, n) { o[i] = 0; }
      ll w = dfs(from, to, inf);
      all += w;
      if (w == 0) {
        break;
      }
    }
    return all;
  }
  ~dinic() {
    delete[] v;
    delete[] l;
    delete[] o;
  }
};

ll gcd(ll a, ll b) {
  if (a > b) {
    return gcd(b, a);
  }
  if (b % a == 0) {
    return a;
  } else {
    return gcd(b % a, a);
  }
}

ll nd[1000], md[1000];

int main() {
  while (1) {
    int n, m;
    scanf("%d%d", &n, &m);
    if (n == 0) {
      break;
    }
    rep(i, n) { scanf("%lld", nd + i); }
    rep(i, m) { scanf("%lld", md + i); }
    dinic s(2 + n + m);
    rep(i, n) { s.add(n + m, i, 1); }
    rep(i, m) { s.add(n + i, n + m + 1, 1); }
    rep(i, n) {
      rep(j, m) {
        if (gcd(nd[i], md[j]) > 1) {
          s.add(i, n + j, 1);
        }
      }
    }
    printf("%lld\\n", s.solve(n + m, n + m + 1));
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <functional>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

#define inf 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)

typedef long long ll;

class path {
public:
  int t;
  ll c;
  int p;
};

class dinic {
public:
  int n;
  vector<path> p;
  vector<int> *v;
  int *l;
  int *o;
  dinic(int nw) {
    n = nw;
    v = new vector<int>[n];
    l = new int[n];
    o = new int[n];
  }
  void add(int from, int to, ll cap) {
    path w1, w2;
    w1.t = to;
    w1.c = cap;
    w1.p = p.size() + 1;
    v[from].push_back(p.size());
    w2.t = from;
    w2.c = 0;
    w2.p = p.size();
    v[to].push_back(p.size() + 1);
    p.push_back(w1);
    p.push_back(w2);
  }
  void dics(int from) {
    rep(i, n) { l[i] = inf; }
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        pq;
    pq.push(make_pair(0, from));
    while (!pq.empty()) {
      int k = pq.top().first;
      int m = pq.top().second;
      pq.pop();
      if (l[m] > k) {
        l[m] = k;
        rep(i, v[m].size()) {
          int x = v[m][i];
          if (p[x].c > 0) {
            pq.push(make_pair(k + 1, p[x].t));
          }
        }
      }
    }
  }
  ll dfs(int now, int to, ll mi) {
    if (now == to) {
      return mi;
    }
    ll all = 0;
    while (o[now] < v[now].size()) {
      int x = v[now][o[now]];
      ll w = dfs(p[x].t, min(mi - all, p[x].c), to);
      p[x].c -= w;
      p[p[x].p].c += w;
      all += w;
      if (mi - all == 0) {
        break;
      }
      o[now]++;
    }
    return all;
  }
  ll solve(int from, int to) {
    ll all = 0;
    while (1) {
      dics(from);
      rep(i, n) { o[i] = 0; }
      int w = dfs(from, to, inf);
      all += w;
      if (w == 0) {
        break;
      }
    }
    return all;
  }
  ~dinic() {
    delete[] v;
    delete[] l;
    delete[] o;
  }
};

ll gcd(ll a, ll b) {
  if (a > b) {
    return gcd(b, a);
  }
  if (b % a == 0) {
    return a;
  } else {
    return gcd(b % a, a);
  }
}

ll nd[1000], md[1000];

int main() {
  while (1) {
    int n, m;
    scanf("%d%d", &n, &m);
    if (n == 0) {
      break;
    }
    rep(i, n) { scanf("%lld", nd + i); }
    rep(i, m) { scanf("%lld", md + i); }
    dinic s(2 + n + m);
    rep(i, n) { s.add(n + m, i, 1); }
    rep(i, m) { s.add(n + i, n + m + 1, 1); }
    rep(i, n) {
      rep(j, m) {
        if (gcd(nd[i], md[j]) > 1) {
          s.add(i, n + j, 1);
        }
      }
    }
    printf("%lld\\n", s.solve(n + m, n + m + 1));
  }
}