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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

long long e, v;

class pass {
public:
  long long t;
  long long c;
  long long p;
};

pass p[250000];

long long noded[2000];
pair<long long, long long> nodef[2000];
vector<long long> node[2000];

void add(long long f, long long t, long long c) {
  p[e].t = t;
  p[e].c = c;
  p[e].p = e + 1;
  p[e + 1].t = f;
  p[e + 1].c = 0;
  p[e + 1].p = e;
  node[f].push_back(e);
  node[t].push_back(e + 1);
  e += 2;
}

void push() {
  priority_queue<pair<long long, long long>> q;
  for (long long i = 0; i < v; i++) {
    noded[i] = 0;
    nodef[i] = make_pair(0, 0);
  }
  noded[0] = v;
  for (auto i : node[0]) {
    if (p[i].c > 0) {
      p[p[i].p].c += p[i].c;
      nodef[p[i].t].first += p[i].c;
      nodef[0].second += p[i].c;
      p[i].c = 0;
      q.push(make_pair(0, p[i].t));
    }
  }
  nodef[v - 1].second = ((long long)1 << 50);
  while (!q.empty()) {
    long long np = q.top().second;
    q.pop();
    while (1) {
      for (auto i : node[np]) {
        if (p[i].c > 0) {
          long long t = p[i].t;
          if (noded[np] == noded[t] + 1) {
            long long kae = min(p[i].c, nodef[np].first - nodef[np].second);
            p[i].c -= kae;
            p[p[i].p].c += kae;
            nodef[np].second += kae;
            bool w = (nodef[t].first <= nodef[t].second);
            nodef[t].first += kae;
            if (w && nodef[t].first > nodef[t].second) {
              q.push(make_pair(noded[t], t));
            }
            if (nodef[np].first <= nodef[np].second) {
              break;
            }
          }
        }
      }
      if (nodef[np].first > nodef[np].second && np != 0) {
        long long minw = ((long long)1 << 50);
        for (auto i : node[np]) {
          if (p[i].c > 0) {
            minw = min(minw, noded[p[i].t]);
          }
        }
        noded[np] = minw + 1;
      } else {
        break;
      }
    }
  }
}

long long dn[2000];
long long dm[2000];

long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

int main() {
  while (1) {
    long long n, m;
    scanf("%lld%lld", &n, &m);
    if (n == 0) {
      break;
    }
    v = n + m + 2;
    e = 0;
    for (long long i = 0; i < n; i++) {
      scanf("%lld", &dn[i]);
      add(0, 1 + i, 1);
    }
    for (long long i = 0; i < m; i++) {
      scanf("%lld", &dm[i]);
      add(n + 1 + i, n + m + 1, 1);
    }
    for (long long i = 0; i < n; i++) {
      for (long long ii = 0; ii < m; ii++) {
        if (1 < gcd(dn[i], dm[ii])) {
          add(1 + i, n + 1 + ii, 1);
        }
      }
    }
    push();
    long long all = 0;
    for (auto i : node[0]) {
      if (i % 2 == 0) {
        all += p[p[i].p].c;
      }
    }
    printf("%lld\\n", all);
    for (long long i = 0; i < v; i++) {
      node[i].clear();
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

long long e, v;

class pass {
public:
  long long t;
  long long c;
  long long p;
};

pass p[250000];

long long noded[1000];
pair<long long, long long> nodef[1000];
vector<long long> node[1000];

void add(long long f, long long t, long long c) {
  p[e].t = t;
  p[e].c = c;
  p[e].p = e + 1;
  p[e + 1].t = f;
  p[e + 1].c = 0;
  p[e + 1].p = e;
  node[f].push_back(e);
  node[t].push_back(e + 1);
  e += 2;
}

void push() {
  priority_queue<pair<long long, long long>> q;
  for (long long i = 0; i < v; i++) {
    noded[i] = 0;
    nodef[i] = make_pair(0, 0);
  }
  noded[0] = v;
  for (auto i : node[0]) {
    if (p[i].c > 0) {
      p[p[i].p].c += p[i].c;
      nodef[p[i].t].first += p[i].c;
      nodef[0].second += p[i].c;
      p[i].c = 0;
      q.push(make_pair(0, p[i].t));
    }
  }
  nodef[v - 1].second = ((long long)1 << 50);
  while (!q.empty()) {
    long long np = q.top().second;
    q.pop();
    while (1) {
      for (auto i : node[np]) {
        if (p[i].c > 0) {
          long long t = p[i].t;
          if (noded[np] == noded[t] + 1) {
            long long kae = min(p[i].c, nodef[np].first - nodef[np].second);
            p[i].c -= kae;
            p[p[i].p].c += kae;
            nodef[np].second += kae;
            bool w = (nodef[t].first <= nodef[t].second);
            nodef[t].first += kae;
            if (w && nodef[t].first > nodef[t].second) {
              q.push(make_pair(noded[t], t));
            }
            if (nodef[np].first <= nodef[np].second) {
              break;
            }
          }
        }
      }
      if (nodef[np].first > nodef[np].second && np != 0) {
        long long minw = ((long long)1 << 50);
        for (auto i : node[np]) {
          if (p[i].c > 0) {
            minw = min(minw, noded[p[i].t]);
          }
        }
        noded[np] = minw + 1;
      } else {
        break;
      }
    }
  }
}

long long dn[1000];
long long dm[1000];

long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

int main() {
  while (1) {
    long long n, m;
    scanf("%lld%lld", &n, &m);
    if (n == 0) {
      break;
    }
    v = n + m + 2;
    e = 0;
    for (long long i = 0; i < n; i++) {
      scanf("%lld", &dn[i]);
      add(0, 1 + i, 1);
    }
    for (long long i = 0; i < m; i++) {
      scanf("%lld", &dm[i]);
      add(n + 1 + i, n + m + 1, 1);
    }
    for (long long i = 0; i < n; i++) {
      for (long long ii = 0; ii < m; ii++) {
        if (1 < gcd(dn[i], dm[ii])) {
          add(1 + i, n + 1 + ii, 1);
        }
      }
    }
    push();
    long long all = 0;
    for (auto i : node[0]) {
      if (i % 2 == 0) {
        all += p[p[i].p].c;
      }
    }
    printf("%lld\\n", all);
    for (long long i = 0; i < v; i++) {
      node[i].clear();
    }
  }
  return 0;
}