Problem p00744 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

struct edge {
  int to, cap, rev;
};

vector<edge> G[10000];
bool used[10000];
void add_edge(int from, int to, int cap) {
  G[from].pb((edge){to, cap, (int)G[to].size()});
  G[to].pb((edge){from, 0, (int)(G[from].size()) - 1});
}

// テ・ツ「ツ療・ツ環?」ツδ妥」ツつケテ」ツつ奪FSテ」ツ?ァテヲツ篠「テ」ツ??
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}
// sテ」ツ?凝」ツつ液テ」ツ?クテ」ツ?ョテヲツ慊?・ツ、ツァテヲツオツ?」ツつ津ヲツアツづ」ツつ?」ツつ?
int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main() {
  int m, n;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    rep(i, 10000) G[i].clear();
    rep(i, 10000) used[i] = false;
    int x[600];
    int y[600];
    rep(i, n) cin >> x[i];
    rep(i, m) cin >> y[i];

    rep(i, n) {
      rep(j, m) {
        if (__gcd(x[i], y[j]) > 1) {
          add_edge(i, n + j, 1);
        }
      }
    }
    int s = n + m, t = s + 1;
    rep(i, n) { add_edge(s, i, 1); }
    rep(i, m) { add_edge(n + i, t, 1); }
    cout << max_flow(s, t) << endl;
  }
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

struct edge {
  int to, cap, rev;
};

vector<edge> G[1000000];
bool used[1000000];
void add_edge(int from, int to, int cap) {
  G[from].pb((edge){to, cap, (int)G[to].size()});
  G[to].pb((edge){from, 0, (int)(G[from].size()) - 1});
}

// テ・ツ「ツ療・ツ環?」ツδ妥」ツつケテ」ツつ奪FSテ」ツ?ァテヲツ篠「テ」ツ??
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}
// sテ」ツ?凝」ツつ液テ」ツ?クテ」ツ?ョテヲツ慊?・ツ、ツァテヲツオツ?」ツつ津ヲツアツづ」ツつ?」ツつ?
int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main() {
  int m, n;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    int x[600];
    int y[600];
    rep(i, n) cin >> x[i];
    rep(i, m) cin >> y[i];

    rep(i, n) {
      rep(j, m) {
        if (__gcd(x[i], y[j]) > 1) {
          add_edge(i, n + j, 1);
        }
      }
    }
    int s = n + m, t = s + 1;
    rep(i, n) { add_edge(s, i, 1); }
    rep(i, m) { add_edge(n + i, t, 1); }
    cout << max_flow(s, t) << endl;
  }
}