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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define LOOP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define MP make_pair
#define FIR first
#define SEC second
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;

typedef int Flow;
struct Edge {
  int src;
  int dst;
  int rev;
  Flow cap;
  Edge(int s = 0, int d = 0, Flow c = 0, int r = 0)
      : src(s), dst(d), cap(c), rev(r) {}
};

using Graph = vector<vector<Edge>>;
Graph g;
int m, n;
bool used[10005];

void add(int src, int dst, int cap) {
  g[src].emplace_back(src, dst, cap, g[dst].size());
  g[dst].emplace_back(dst, src, 0, g[src].size() - 1);
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  REP(i, g[v].size()) {
    Edge &e = g[v][i];
    if (!used[e.dst] && e.cap > 0) {
      int d = dfs(e.dst, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        g[e.dst][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int maximum_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    fill(used, used + n + m + 10, 0);
    int f = dfs(s, t, INF);
    if (!f)
      return flow;
    flow += f;
  }
}
int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

signed main() {
  while (cin >> m >> n, m) {
    int b, r;
    g = Graph(m + n + 10);
    vector<pair<int, int>> B, R;
    REP(i, m) {
      cin >> b;
      B.PB(MP(i + 1, b));
    }
    REP(i, n) {
      cin >> r;
      R.PB(MP(i + 1 + m, r));
    }
    REP(i, m) add(0, i + 1, 1);
    REP(i, n) add(m + i + 1, n + m + 1, 1);
    REP(i, m) REP(j, n) {
      if (gcd(B[i].SEC, R[j].SEC) > 1) {
        add(B[i].FIR, R[j].FIR, 1);
      }
    }
    printf("%d\\n", maximum_flow(0, n + m + 1));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define LOOP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define MP make_pair
#define FIR first
#define SEC second
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;

typedef int Flow;
struct Edge {
  int src;
  int dst;
  int rev;
  Flow cap;
  Edge(int s = 0, int d = 0, Flow c = 0, int r = 0)
      : src(s), dst(d), cap(c), rev(r) {}
};

using Graph = vector<vector<Edge>>;
Graph g;
int m, n;
bool used[10005];

void add(int src, int dst, int cap) {
  g[src].emplace_back(src, dst, cap, g[dst].size());
  g[dst].emplace_back(dst, src, 0, g[src].size() - 1);
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  REP(i, g[v].size()) {
    Edge &e = g[v][i];
    if (!used[e.dst] && e.cap > 0) {
      int d = dfs(e.dst, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        g[e.dst][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int maximum_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    fill(used, used + n + m + 10, 0);
    int f = dfs(s, t, INF);
    if (!f)
      return flow;
    flow += f;
  }
}

signed main() {
  while (cin >> m >> n, m) {
    int b, r;
    g = Graph(m + n + 10);
    vector<pair<int, int>> B, R;
    REP(i, m) {
      cin >> b;
      B.PB(MP(i + 1, b));
    }
    REP(i, n) {
      cin >> r;
      R.PB(MP(i + 1 + m, r));
    }
    REP(i, m) add(0, i + 1, 1);
    REP(i, n) add(m + i + 1, n + m + 1, 1);
    REP(i, m) REP(j, n) {
      int tmp = min(B[i].SEC, R[j].SEC);
      LOOP(k, 2, tmp + 1) {
        if (B[i].SEC % k == 0 && R[j].SEC % k == 0) {
          add(B[i].FIR, R[j].FIR, 1);
        }
      }
    }
    cout << maximum_flow(0, m + n + 1) << endl;
  }
  return 0;
}