Language: C++

#include <bits/stdc++.h>
#define pii pair <int, int>

using namespace std;

const int N = 2005, M = 4005;
int n, a[N], edge[N][N], ans[N], root[N], size[N], b[N], ed[N], fa[N];
bool vis[N];

int gcd(int x, int y) {
    if (y == 0) return x;
    return gcd(y, x % y);
}


void dfs(int x) {
    vis[x] = 1;
    size[x] = 1;
    for (int xx = 1; xx <= n; xx++) {
        if (edge[x][xx] && !vis[xx]) {
            dfs(xx);
            size[x] += size[xx];
        }
    }
}

void solve(int x, int l, int r)
{
    priority_queue <pii> q;
    vis[x] = 1;
    int now = l;
    for (int xx = 1; xx <= n; xx++) {
        if (edge[x][xx] && !vis[xx]) {
            solve(xx, now + 1, now + size[xx]);
            q.push(pii(ans[now + 1], now + 1));
            now += size[xx];
        }
    }
    now = 0;
    while (!q.empty()) {
        pii xx = q.top();
        q.pop();
        b[++now] = xx.first;
        if (xx.second < ed[xx.second]) {
            q.push(pii(ans[xx.second + 1], xx.second + 1));
        }
    }
    ans[l] = a[x];
    ed[l] = r;
    for (int i = l + 1; i <= r; i++) {
        ans[i] = b[i - l];
        ed[i] = r;
    }
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            if (gcd(a[i], a[j]) > 1) {
                edge[i][j] = 1;
                edge[j][i] = 1;
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            dfs(i);
            edge[0][i] = 1;
            edge[i][0] = 1;
        }
    }
    memset(vis, 0, sizeof(vis));
    solve(0, 0, n);
    for (int i = 1; i < n; i++) {
        cout << ans[i] << " ";
    }
    cout << ans[n] << endl;
    return 0;
}

Compiler output:
a.cc: In function 'void dfs(int)':
a.cc:18:5: error: reference to 'size' is ambiguous
   18 |     size[x] = 1;
      |     ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:43: note:                 'int size [2005]'
    7 | int n, a[N], edge[N][N], ans[N], root[N], size[N], b[N], ed[N], fa[N];
      |                                           ^~~~
a.cc:22:13: error: reference to 'size' is ambiguous
   22 |             size[x] += size[xx];
      |             ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:43: note:                 'int size [2005]'
    7 | int n, a[N], edge[N][N], ans[N], root[N], size[N], b[N], ed[N], fa[N];
      |                                           ^~~~
a.cc:22:24: error: reference to 'size' is ambiguous
   22 |             size[x] += size[xx];
      |                        ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:43: note:                 'int size [2005]'
    7 | int n, a[N], edge[N][N], ans[N], root[N], size[N], b[N], ed[N], fa[N];
      |                                           ^~~~
a.cc: In function 'void solve(int, int, int)':
a.cc:34:38: error: reference to 'size' is ambiguous
   34 |             solve(xx, now + 1, now + size[xx]);
      |                                      ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:43: note:                 'int size [2005]'
    7 | int n, a[N], edge[N][N], ans[N], root[N], size[N], b[N], ed[N], fa[N];
      |                                           ^~~~
a.cc:36:20: error: reference to 'size' is ambiguous
   36 |             now += size[xx];
      |                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:43: note:                 'int size [2005]'
    7 | int n, a[N], edge[N][N], ans[N], root[N], size[N], b[N], ed[N], fa[N];
      |                                           ^~~~