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

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

long long int v;
vector<vector<long long int>> g;
vector<long long int> match;
bool used[1001];

long long int gcd(long long int x, long long int y) {

  if (y > x)
    swap(x, y);

  while (y > 0) {

    long long int k = x % y;
    x = y;
    y = k;
  }

  return x;
}

bool dfs(long long int x) {

  used[x] = true;

  for (long long int i = 0; i < g[x].size(); i++) {

    long long int k = g[x][i];
    long long int w = match[k];

    if (w < 0 || !used[w] && dfs(w)) {

      match[x] = k;
      match[k] = x;
      return true;
    }
  }

  return false;
}

int main() {

  while (true) {

    long long int m, n;
    cin >> m >> n;
    if (m == 0)
      break;
    v = m + n;

    vector<long long int> b, r;

    for (long long int i = 0; i < m; i++) {
      long long int in;
      cin >> in;
      b.push_back(in);
    }

    for (long long int i = 0; i < n; i++) {
      long long int in;
      cin >> in;
      r.push_back(in);
    }

    g.clear();
    for (long long int i = 0; i < v; i++) {
      vector<long long int> vec;
      g.push_back(vec);
    }

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

    long long int ans = 0;

    match.clear();
    for (long long int i = 0; i < v; i++) {
      match.push_back(-1);
    }

    for (long long int x = 0; x < v; x++) {

      if (match[x] < 0) {

        for (long long int j = 0; j < v; j++) {
          used[j] = false;
        }

        if (dfs(x) == true) {
          ans++;
        }
      }
    }

    cout << ans << endl;

    /*
    for ( long long int i = 0; i < m; i++ ) {
      g[i].push_back( m+n );
    }
    for ( long long int i = 0; i < n; i++ ) {
      g[m+i].push_back( m+n+1 );
    }
    */
  }

  return 0;
}

Error: TLE

Fixed submission:

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

long long int v;
vector<vector<long long int>> g;
vector<long long int> match;
bool used[1001];

bool dfs(long long int x) {

  used[x] = true;

  for (long long int i = 0; i < g[x].size(); i++) {

    long long int k = g[x][i];
    long long int w = match[k];

    if (w < 0 || !used[w] && dfs(w)) {

      match[x] = k;
      match[k] = x;
      return true;
    }
  }

  return false;
}

int main() {

  bool so[10000] = {};
  vector<long long int> sosu;

  for (long long int i = 2; i < 10000; i++) {
    if (so[i] == false) {
      sosu.push_back(i);
      for (long long int j = 2; j * i < 10000; j++) {
        so[j * i] = true;
      }
    }
  }

  while (true) {

    long long int m, n;
    cin >> m >> n;
    if (m == 0)
      break;
    v = m + n;

    vector<long long int> b, r;

    for (long long int i = 0; i < m; i++) {
      long long int in;
      cin >> in;
      b.push_back(in);
    }

    for (long long int i = 0; i < n; i++) {
      long long int in;
      cin >> in;
      r.push_back(in);
    }

    g.clear();
    for (long long int i = 0; i < v; i++) {
      vector<long long int> vec;
      g.push_back(vec);
    }

    for (long long int i = 0; i < m; i++) {
      for (long long int j = 0; j < n; j++) {
        bool f = false;
        for (long long int k = 0; k < sosu.size(); k++) {
          if ((b[i] % sosu[k]) == 0 && (r[j] % sosu[k]) == 0) {
            f = true;
            break;
          }
        }
        if (f == true) {
          g[i].push_back(j + m);
          g[j + m].push_back(i);
        }
      }
    }

    long long int ans = 0;

    match.clear();
    for (long long int i = 0; i < v; i++) {
      match.push_back(-1);
    }

    for (long long int x = 0; x < v; x++) {

      if (match[x] < 0) {

        for (long long int j = 0; j < v; j++) {
          used[j] = false;
        }

        if (dfs(x) == true) {
          ans++;
        }
      }
    }

    cout << ans << endl;

    /*
    for ( long long int i = 0; i < m; i++ ) {
      g[i].push_back( m+n );
    }
    for ( long long int i = 0; i < n; i++ ) {
      g[m+i].push_back( m+n+1 );
    }
    */
  }

  return 0;
}