Problem p03811 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <utility>
#include <vector>
#define ll long long
#define mp make_pair
using namespace std;
const int INF = 1e9;
/*--------main part---------*/
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
int n;
int dat[10005];
bool vis[10005];
vector<int> ans[10005];
bool edg[1005][1005];
int a[100005];
void dfs(int x) {
  if (vis[x])
    return;
  vis[x] = true;
  vector<int> chi;
  for (int i = 0; i < n; i++) {
    if (edg[x][i] and !vis[i]) {
      chi.push_back(i);
      dfs(i);
    }
  }
  memset(a, 0, sizeof(a));
  ans[x].push_back(dat[x]);
  while (true) {
    int mx = 0;
    int pos;
    for (int i = 0; i < chi.size(); i++) {
      if (a[i] < ans[chi[i]].size() and ans[chi[i]][a[i]] > mx) {
        mx = ans[chi[i]][a[i]];
        pos = i;
      }
    }
    if (mx == 0)
      break;
    ans[x].push_back(mx);
    a[pos]++;
  }
}
int gcd(int x, int y) {
  if (x == 0) {
    return y;
  } else {
    return gcd(y % x, x);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> dat[i];
  }
  sort(dat, dat + n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (gcd(dat[i], dat[j]) != 1) {
        edg[i][j] = edg[j][i] = true;
      }
    }
    edg[i][n] = edg[n][i] = true;
  }
  dfs(n);
  for (int i = 1; i <= n; i++) {
    cout << ans[n][i] << ' ';
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <utility>
#include <vector>
#define ll long long
#define mp make_pair
using namespace std;
const int INF = 1e9;
/*--------main part---------*/
inline int read() {
  int X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
int n;
int dat[10005];
bool vis[10005];
vector<int> ans[10005];
bool edg[2005][2005];
int a[100005];
void dfs(int x) {
  if (vis[x])
    return;
  vis[x] = true;
  vector<int> chi;
  for (int i = 0; i < n; i++) {
    if (edg[x][i] and !vis[i]) {
      chi.push_back(i);
      dfs(i);
    }
  }
  memset(a, 0, sizeof(a));
  ans[x].push_back(dat[x]);
  while (true) {
    int mx = 0;
    int pos;
    for (int i = 0; i < chi.size(); i++) {
      if (a[i] < ans[chi[i]].size() and ans[chi[i]][a[i]] > mx) {
        mx = ans[chi[i]][a[i]];
        pos = i;
      }
    }
    if (mx == 0)
      break;
    ans[x].push_back(mx);
    a[pos]++;
  }
}
int gcd(int x, int y) {
  if (x == 0) {
    return y;
  } else {
    return gcd(y % x, x);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> dat[i];
  }
  sort(dat, dat + n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (gcd(dat[i], dat[j]) != 1) {
        edg[i][j] = edg[j][i] = true;
      }
    }
    edg[i][n] = edg[n][i] = true;
  }
  dfs(n);
  for (int i = 1; i <= n; i++) {
    cout << ans[n][i] << ' ';
  }
  return 0;
}