Problem p03811 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define ll long long
#define rep(i, a, b) for (int i = a; i <= (int)b; i++)
void read(int &x) {
  x = 0;
  int flag = 1;
  char ch = ' ';
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
void read(ll &x) {
  x = 0;
  int flag = 1;
  char ch = ' ';
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
using namespace std;
const int maxn = 1010;
int n, degree[maxn], a[maxn];
bool vis[maxn], mp[maxn][maxn];
vector<int> ans, edge[maxn];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void dfs(int u) {
  vis[u] = 1;
  rep(i, 1, n) if (!vis[i] && mp[u][i]) edge[u].push_back(i), degree[i]++,
      dfs(i);
}
void solve() {
  priority_queue<int> pq;
  memset(vis, 0, sizeof(vis));
  rep(i, 1, n) if (degree[i] == 0) pq.push(i), vis[i] = 1;
  while (!pq.empty()) {
    int u = pq.top();
    pq.pop();
    ans.push_back(u);
    rep(i, 0, edge[u].size() - 1) {
      int v = edge[u][i];
      if (--degree[v] == 0)
        pq.push(v);
    }
  }
}
int main() {
  read(n);
  rep(i, 1, n) read(a[i]);
  sort(a + 1, a + 1 + n);
  rep(i, 1, n - 1) rep(j, i + 1, n) if (gcd(a[i], a[j]) != 1) mp[i][j] =
      mp[j][i] = 1;
  rep(i, 1, n) dfs(i);
  solve();
  rep(i, 0, n - 1) printf("%d ", a[ans[i]]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define ll long long
#define rep(i, a, b) for (int i = a; i <= (int)b; i++)
void read(int &x) {
  x = 0;
  int flag = 1;
  char ch = ' ';
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
void read(ll &x) {
  x = 0;
  int flag = 1;
  char ch = ' ';
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
using namespace std;
const int maxn = 2010;
int n, degree[maxn], a[maxn];
bool vis[maxn], mp[maxn][maxn];
vector<int> ans, edge[maxn];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void dfs(int u) {
  vis[u] = 1;
  rep(i, 1, n) if (!vis[i] && mp[u][i]) edge[u].push_back(i), degree[i]++,
      dfs(i);
}
void solve() {
  priority_queue<int> pq;
  memset(vis, 0, sizeof(vis));
  rep(i, 1, n) if (degree[i] == 0) pq.push(i), vis[i] = 1;
  while (!pq.empty()) {
    int u = pq.top();
    pq.pop();
    ans.push_back(u);
    rep(i, 0, edge[u].size() - 1) {
      int v = edge[u][i];
      if (--degree[v] == 0)
        pq.push(v);
    }
  }
}
int main() {
  read(n);
  rep(i, 1, n) read(a[i]);
  sort(a + 1, a + 1 + n);
  rep(i, 1, n - 1) rep(j, i + 1, n) if (gcd(a[i], a[j]) != 1) mp[i][j] =
      mp[j][i] = 1;
  rep(i, 1, n) dfs(i);
  solve();
  rep(i, 0, n - 1) printf("%d ", a[ans[i]]);
  return 0;
}