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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int bipartiteMaximumMatchings(const vector<vector<bool>> &graph) {
  int answer = 0;
  int n = graph.size();
  int m = graph[0].size();
  vector<int> selectN(n, -1);
  vector<int> selectM(m, -1);

  for (int i = 0; i < n; ++i) {
    vector<bool> visitedN(n, false);
    vector<bool> visitedM(m, false);
    visitedN[i] = true;
    vector<int> path(min(n, m) * 2, -1);

    path[0] = i;
    int index = 1;
    for (;;) {
      while (++path[index] < m &&
             (!graph[path[index - 1]][path[index]] ||
              selectN[path[index - 1]] == path[index] || visitedM[path[index]]))
        ;
      if (path[index] < m) {
        if (selectM[path[index]] == -1) {
          for (int j = 0; j < index; j += 2) {
            selectN[path[j]] = path[j + 1];
            selectM[path[j + 1]] = path[j];
          }
          ++answer;
          break;
        }
        if (!visitedN[selectM[path[index]]]) {
          path[index + 1] = selectM[path[index]];
          visitedM[path[index]] = true;
          visitedN[path[index + 1]] = true;
          index += 2;
        }
      } else {
        path[index] = -1;
        index -= 2;
        if (index < 0)
          break;
      }
    }

    if (answer == m)
      break;
  }

  return answer;
}

void solve(const vector<int> &blue, const vector<int> &red) {
  vector<vector<bool>> graph(blue.size(), vector<bool>(red.size(), false));
  for (unsigned i = 0; i < blue.size(); ++i) {
    vector<int> num;
    int a = blue[i];
    int b = 2;
    while (a >= b * b) {
      if (a % b == 0) {
        num.push_back(b);
        while (a % b == 0)
          a /= b;
      }
      ++b;
    }
    if (a > 1)
      num.push_back(a);

    for (unsigned j = 0; j < red.size(); ++j) {
      for (unsigned k = 0; k < num.size(); ++k) {
        if (red[j] % num[k] == 0) {
          graph[i][j] = true;
          break;
        }
      }
    }
  }

  cout << bipartiteMaximumMatchings(graph) << endl;
}

int main() {
  for (;;) {
    int m, n;
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;

    vector<int> blue(m);
    vector<int> red(n);
    for (int i = 0; i < m; ++i)
      cin >> blue[i];
    for (int i = 0; i < n; ++i)
      cin >> red[i];

    solve(blue, red);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int bipartiteMaximumMatchings(const vector<vector<bool>> &graph) {
  int answer = 0;
  int n = graph.size();
  int m = graph[0].size();
  vector<int> selectN(n, -1);
  vector<int> selectM(m, -1);

  for (int i = 0; i < n; ++i) {
    vector<bool> visitedN(n, false);
    vector<bool> visitedM(m, false);
    visitedN[i] = true;
    vector<int> path(min(n, m) * 2, -1);

    path[0] = i;
    int index = 1;
    for (;;) {
      while (++path[index] < m &&
             (!graph[path[index - 1]][path[index]] ||
              selectN[path[index - 1]] == path[index] || visitedM[path[index]]))
        ;
      if (path[index] < m) {
        if (selectM[path[index]] == -1) {
          for (int j = 0; j < index; j += 2) {
            selectN[path[j]] = path[j + 1];
            selectM[path[j + 1]] = path[j];
          }
          ++answer;
          break;
        }
        if (!visitedN[selectM[path[index]]]) {
          path[index + 1] = selectM[path[index]];
          visitedM[path[index]] = true;
          visitedN[path[index + 1]] = true;
          index += 2;
        }
      } else {
        path[index] = -1;
        index -= 2;
        if (index < 0)
          break;
      }
    }
  }

  return answer;
}

void solve(const vector<int> &blue, const vector<int> &red) {
  vector<vector<bool>> graph(blue.size(), vector<bool>(red.size(), false));
  for (unsigned i = 0; i < blue.size(); ++i) {
    vector<int> num;
    int a = blue[i];
    int b = 2;
    while (a >= b * b) {
      if (a % b == 0) {
        num.push_back(b);
        while (a % b == 0)
          a /= b;
      }
      ++b;
    }
    if (a > 1)
      num.push_back(a);

    for (unsigned j = 0; j < red.size(); ++j) {
      for (unsigned k = 0; k < num.size(); ++k) {
        if (red[j] % num[k] == 0) {
          graph[i][j] = true;
          break;
        }
      }
    }
  }

  cout << bipartiteMaximumMatchings(graph) << endl;
}

int main() {
  for (;;) {
    int m, n;
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;

    vector<int> blue(m);
    vector<int> red(n);
    for (int i = 0; i < m; ++i)
      cin >> blue[i];
    for (int i = 0; i < n; ++i)
      cin >> red[i];

    solve(blue, red);
  }

  return 0;
}