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

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

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

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

#define MAX_V 1000010

struct Edge {
  int to, cap, rev;
  Edge(){};
  Edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev){};
};

vector<Edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap) {
  G[from].push_back({to, cap, (int)G[to].size()});
  G[to].push_back({from, 0, (int)G[from].size() - 1});
}

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;
}

int max_flow(int s, int t) { // O(FE);
  int flow = 0;
  while (1) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    for (int i = 0; i < MAX_V; i++)
      G[i].clear(), used[i] = false;
    vector<int> b(n), r(m);
    rep(i, n) cin >> b[i];
    rep(i, m) cin >> r[i];
    set<int> bst[510], rst[510];
    rep(i, n) {
      for (int j = 2; (ll)(j * j) <= b[i]; j++) {
        if (b[i] % j == 0)
          bst[i].insert(j);
        while (b[i] % j == 0)
          b[i] /= j;
      }
      if (b[i] != 1)
        bst[i].insert(b[i]);
    }
    rep(i, m) {
      for (int j = 2; (ll)(j * j) <= r[i]; j++) {
        if (r[i] % j == 0)
          rst[i].insert(j);
        while (r[i] % j == 0)
          r[i] /= j;
      }
      if (r[i] != 1)
        rst[i].insert(r[i]);
    }
    int s = n + m, t = n + m + 1;
    rep(i, n) add_edge(s, i, 1);
    rep(i, m) add_edge(n + i, t, 1);
    rep(i, n) rep(j, m) {
      for (auto &p : bst[i]) {
        if (rst[j].count(p)) {
          add_edge(i, n + j, 1);
          break;
        }
      }
    }
    cout << max_flow(s, t) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

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

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

#define MAX_V 1000010

struct Edge {
  int to, cap, rev;
  Edge(){};
  Edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev){};
};

vector<Edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap) {
  G[from].push_back({to, cap, (int)G[to].size()});
  G[to].push_back({from, 0, (int)G[from].size() - 1});
}

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;
}

int max_flow(int s, int t) { // O(FE);
  int flow = 0;
  while (1) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main() {
  int n, m;
  while (cin >> n >> m && n && m) {
    for (int i = 0; i < MAX_V; i++)
      G[i].clear(), used[i] = false;
    vector<int> b(n), r(m);
    rep(i, n) cin >> b[i];
    rep(i, m) cin >> r[i];
    set<int> bst[510], rst[510];
    rep(i, n) {
      for (int j = 2; j < b[i]; j++) {
        if (b[i] % j == 0)
          bst[i].insert(j);
        while (b[i] % j == 0)
          b[i] /= j;
      }
      if (b[i] != 1)
        bst[i].insert(b[i]);
    }
    rep(i, m) {
      for (int j = 2; j < r[i]; j++) {
        if (r[i] % j == 0)
          rst[i].insert(j);
        while (r[i] % j == 0)
          r[i] /= j;
      }
      if (r[i] != 1)
        rst[i].insert(r[i]);
    }
    int s = n + m, t = n + m + 1;
    rep(i, n) add_edge(s, i, 1);
    rep(i, m) add_edge(n + i, t, 1);
    rep(i, n) rep(j, m) {
      for (auto &p : bst[i]) {
        if (rst[j].count(p)) {
          add_edge(i, n + j, 1);
          break;
        }
      }
    }
    cout << max_flow(s, t) << endl;
  }
}