Problem p00744 (C++) — buggy submission, verdict: Runtime Error

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

struct edge {
  int to;
  int from;
  int cap;
  int rev;
};
const int max_v = 1010;
int s = 0, t;
bool visited[max_v];
vector<edge> g[max_v];
int red_card[510], blue_card[510];

int max_flow();
int get_card();
void print_g();
int dfs(int node, int f);

void clear() {
  for (int i = 0; i < max_v; i++) {
    g[i].clear();
  }
}

int main() {
  while (true) {
    clear();
    if (get_card() == -1) {
      break;
    }
    cout << max_flow() << endl;
    // print_g();
  }
  return 0;
}

void print_g() {
  for (int i = 0; i <= t; i++) {
    for (int j = 0; j < g[i].size(); j++) {
      if (g[i][j].cap != 0) {
        cout << "{" << g[i][j].from << g[i][j].to << g[i][j].cap << g[i][j].rev
             << "}";
      }
    }
    cout << endl;
  }
}

int max_flow() {
  int sum = 0, f = 0;

  while (1) {
    fill(visited, visited + max_v, false);
    f = dfs(s, INT_MAX / 3);

    if (f == 0) {
      break;
    }
    sum += f;
  }
  return sum;
}

int dfs(int node, int f) {
  //  cout << "node:" <<node << " f:" << f;
  int d = 0;

  if (node == t) // goal
  {
    //  cout <<"f:"<< f << endl;
    return f;
  } else if (visited[node] == true) // if visited
  {
    return -1;
  } else {
    visited[node] = true; // visited
  }

  for (int i = 0; i < g[node].size(); i++) // root form node
  {
    edge &e = g[node][i];
    // cout <<" e,to:"  <<e.to <<endl;
    if (e.cap != 0) {
      d = dfs(e.to, min(f, e.cap));
      /*if(dfs(g[node][i].to, f) == 1)
        {
        return 1;
        }*/
      //   cout << "node:" << node << " f:" << f << " d:" << d << endl;
      if (d > 0) {
        e.cap -= d;
        g[e.to][e.rev].cap += d;

        return d;
      }
    }
  }
  return 0;
}

void add_edge(int i, int j) {
  g[i].push_back({j, i, 1, (int)g[j].size()});
  g[j].push_back({i, j, 0, (int)g[i].size() - 1});
}

int get_card() {
  int red, blue;
  cin >> blue >> red;
  if (blue == 0) {
    return -1;
  }
  for (int i = 0; i < blue; i++) {
    cin >> blue_card[i];
  }
  for (int i = 0; i < red; i++) {
    cin >> red_card[i];
  }

  for (int i = 0; i < blue; i++) {
    for (int j = 0; j < red; j++) {
      if (__gcd(blue_card[i], red_card[j]) != 1) { // to from cap rev
        add_edge(i + 1, j + blue + 1);
      }
    }
  }

  for (int i = 1; i <= blue; i++) {
    add_edge(0, i);
  }

  t = blue + red + 1;
  for (int i = blue + 1; i < t; i++) {
    add_edge(i, t);
  }

  /*     for(int i=0;i<=t;i++)
   {
     for(int j=0;j<g[i].size();j++)
       {
         cout << "{" << g[i][j].from << g[i][j].to << g[i][j].cap << g[i][j].rev
   << "}";
       }
     cout << endl;
     }*/
  return 0;
}

Error: 0

Fixed submission:

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

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

int s = 0, t;
bool visited[1000];
vector<edge> g[1000];
int red_card[500], blue_card[500];

int max_flow();
int get_card();
void print_g();
int dfs(int node, int f);

void clear() {
  for (int i = 0; i < 1000; i++) {
    g[i].clear();
  }
}

int main() {
  while (true) {
    clear();
    if (get_card() == -1) {
      break;
    }
    cout << max_flow() << endl;
    // print_g();
  }
  return 0;
}

void print_g() {
  for (int i = 0; i <= t; i++) {
    for (int j = 0; j < g[i].size(); j++) {
      if (g[i][j].cap != 0) {
        cout << "{" << g[i][j].from << g[i][j].to << g[i][j].cap << g[i][j].rev
             << "}";
      }
    }
    cout << endl;
  }
}

int max_flow() {
  int sum = 0, f = 0;

  while (1) {
    fill(visited, visited + 1000, false);
    f = dfs(s, INT_MAX / 3);

    if (f == 0) {
      break;
    }
    sum += f;
  }
  return sum;
}

int dfs(int node, int f) {
  //  cout << "node:" <<node << " f:" << f;
  int d = 0;

  if (node == t) // goal
  {
    //  cout <<"f:"<< f << endl;
    return f;
  } else if (visited[node] == true) // if visited
  {
    return -1;
  } else {
    visited[node] = true; // visited
  }

  for (int i = 0; i < g[node].size(); i++) // root form node
  {
    edge &e = g[node][i];
    // cout <<" e,to:"  <<e.to <<endl;
    if (e.cap != 0) {
      d = dfs(e.to, min(f, e.cap));
      /*if(dfs(g[node][i].to, f) == 1)
        {
        return 1;
        }*/
      //   cout << "node:" << node << " f:" << f << " d:" << d << endl;
      if (d > 0) {
        e.cap -= d;
        g[e.to][e.rev].cap += d;

        return d;
      }
    }
  }
  return 0;
}

void add_edge(int i, int j) {
  g[i].push_back({j, i, 1, (int)g[j].size()});
  g[j].push_back({i, j, 0, (int)g[i].size() - 1});
}

int get_card() {
  int red, blue;
  cin >> blue >> red;
  if (blue == 0) {
    return -1;
  }
  for (int i = 0; i < blue; i++) {
    cin >> blue_card[i];
  }
  for (int i = 0; i < red; i++) {
    cin >> red_card[i];
  }

  for (int i = 0; i < blue; i++) {
    for (int j = 0; j < red; j++) {
      if (__gcd(blue_card[i], red_card[j]) != 1) { // to from cap rev
        add_edge(i + 1, j + blue + 1);
        // g[i+1].push_back({j+blue+1, i+1, 1,(int)g[j+1].size()});
        // g[j+1].push_back({i+1, j+blue+1, 0, (int)g[i+1].size()-1});
      }
    }
  }

  for (int i = 1; i <= blue; i++) {
    add_edge(0, i);
    // g[0].push_back({i, 0, 1, (int)g[i].size()});
    // g[i].push_back({0, i, 0, (int)g[0].size()-1});
  }

  t = blue + red + 1;
  for (int i = blue + 1; i < t; i++) {
    add_edge(i, t);
    // g[i].push_back({t, i, 1, (int)g[t].size()});
    //  g[t].push_back({i, t, 0, (int)g[i].size()-1});
  }

  /*  for(int i=0;i<=t;i++)
   {
     for(int j=0;j<g[i].size();j++)
       {
         cout << "{" << g[i][j].from << g[i][j].to << g[i][j].cap << g[i][j].rev
   << "}";
       }
     cout << endl;
   }
  */
  return 0;
}