Problem p00744 (C++)

Buggy submission (status: Runtime Error):
// Hopcroft-Karp Algorithm
// O(|E| * |V|^(1/2))
// http://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm
// aoj 1163

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int NIL;
const int inf = 1 << 29;

typedef vector<int> vi;
typedef vector<vector<int>> Graph;

bool bfs(const Graph &g, vi &dist, const vi &pair, int m) {
  queue<int> q;

  for (int i = 0; i < m; ++i)
    if (pair[i] == NIL) {
      dist[i] = 0;
      q.push(i);
    } else
      dist[i] = inf;
  dist[NIL] = inf;

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    if (v == NIL)
      continue;
    for (int i = 0; i < g[v].size(); ++i) {
      int u = g[v][i];
      if (dist[pair[u]] == inf) {
        dist[pair[u]] = dist[v] + 1;
        q.push(pair[u]);
      }
    }
  }
  return dist[NIL] != inf;
}

bool dfs(int v, const Graph &g, vi &dist, vi &pair) {
  for (int i = 0; i < g[v].size(); ++i) {
    int u = g[v][i];
    if (dist[pair[u]] == dist[v] + 1) {
      if (dfs(pair[u], g, dist, pair)) {
        pair[u] = v, pair[v] = u;
        return true;
      }
    }
  }
  dist[v] = inf;
  return false;
}

// 0ツつゥツづァm-1ツづ慊づづ個静淞点ツづツつゥツづァn-1ツづ慊づづ個静淞点ツづづ個催妥・ツマツッツチツδ督グツづーツ仰づ淞づゥ
int hopcroft_karp(Graph &graph, int m) {
  const int n = graph.size();
  NIL = n;
  vi pair(n + 1, NIL), dist(n + 1, 0);
  int ret = 0;
  while (bfs(graph, dist, pair, m)) {
    for (int i = 0; i < m; ++i)
      if (pair[i] == NIL)
        if (i == NIL || dfs(i, graph, dist, pair))
          ret++;
  }
  return ret;
}

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int main() {
  ios::sync_with_stdio(false);
  int m, n;
  while (cin >> m >> n, m | n) {
    Graph g(m + n);
    vector<int> b(m), r(n);
    for (int i = 0; i < m; ++i)
      cin >> b[i];
    for (int i = 0; i < n; ++i)
      cin >> r[i];

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        if (gcd(b[i], r[j]) > 1)
          g[i].push_back(j + m);
    cout << hopcroft_karp(g, m) << endl;
  }
}

Fixed submission:
// Hopcroft-Karp Algorithm
// O(|E| * |V|^(1/2))
// http://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm
// aoj 1163

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int NIL;
const int inf = 1 << 29;

typedef vector<int> vi;
typedef vector<vector<int>> Graph;

bool bfs(const Graph &g, vi &dist, const vi &pair, int m) {
  queue<int> q;

  for (int i = 0; i < m; ++i)
    if (pair[i] == NIL) {
      dist[i] = 0;
      q.push(i);
    } else
      dist[i] = inf;
  dist[NIL] = inf;

  while (!q.empty()) {
    int v = q.front();
    q.pop();
    if (v == NIL)
      continue;
    for (int i = 0; i < g[v].size(); ++i) {
      int u = g[v][i];
      if (dist[pair[u]] == inf) {
        dist[pair[u]] = dist[v] + 1;
        q.push(pair[u]);
      }
    }
  }
  return dist[NIL] != inf;
}

bool dfs(int v, const Graph &g, vi &dist, vi &pair) {
  for (int i = 0; i < g[v].size(); ++i) {
    int u = g[v][i];
    if (dist[pair[u]] == dist[v] + 1) {
      if (pair[u] == NIL || dfs(pair[u], g, dist, pair)) {
        pair[u] = v, pair[v] = u;
        return true;
      }
    }
  }
  dist[v] = inf;
  return false;
}

// 0ツつゥツづァm-1ツづ慊づづ個静淞点ツづツつゥツづァn-1ツづ慊づづ個静淞点ツづづ個催妥・ツマツッツチツδ督グツづーツ仰づ淞づゥ
int hopcroft_karp(Graph &graph, int m) {
  const int n = graph.size();
  NIL = n;
  vi pair(n + 1, NIL), dist(n + 1, 0);
  int ret = 0;
  while (bfs(graph, dist, pair, m)) {
    for (int i = 0; i < m; ++i)
      if (pair[i] == NIL)
        if (i == NIL || dfs(i, graph, dist, pair))
          ret++;
  }
  return ret;
}

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int main() {
  ios::sync_with_stdio(false);
  int m, n;
  while (cin >> m >> n, m | n) {
    Graph g(m + n);
    vector<int> b(m), r(n);
    for (int i = 0; i < m; ++i)
      cin >> b[i];
    for (int i = 0; i < n; ++i)
      cin >> r[i];

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        if (gcd(b[i], r[j]) > 1)
          g[i].push_back(j + m);
    cout << hopcroft_karp(g, m) << endl;
  }
}