Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define N 2010
#define chmax(a,b) a=max(a,b);
vector<ll> g[N];
ll a[N],n;
vector<ll> t[N];
ll nz[N];
ll gcd(ll a,ll b){
    if(a<b)swap(a,b);
    if(b==0)return a;
    a%=b;
    return gcd(a,b);
}

void Solve(){
 for(int i=0;i<n;i++)nz[i]=0;
 for(int i=0;i<n;i++){
  for(auto x:g[i])nz[x]++;   
 }
 priority_queue<ll> Q;
 vector<ll> ans;
 for(int i=0;i<n;i++){
     if(nz[i]==0)Q.push(make_pair(a[i],i));
 }
 while(!Q.empty()){
  ll x=Q.top().second;
  Q.pop();
  ans.push_back(a[x]);
  for(auto y:t[x]){
   nz[y]--;
   if(nz[y]==0)Q.push(make_pair(a[y],y));
  }
 }
 for(int i=0;i<n;i++){
  if(i)cout<<" ";
  cout<<ans[i];
 }cout<<endl;
}

ll maxfrom[N];
priority_queue<ll>Q;
void bfs(ll x){
    if(vis[x])return; vis[x]=1;
    if(~maxfrom[x])t[maxfrom[x]].push_back(x);
    for(auto y:g[x]){
        chmax(maxfrom[y],x);
        Q.push(y);
    }
}
int main(){
 cin>>n;
 for(int i=0;i<n;i++)cin>>a[i];
 sort(a,a+n);
 for(int i=0;i<n;i++)nz[i]=0;
 for(int i=0;i<n;i++){
  for(int j=0;j<i;j++){
   if(gcd(a[i],a[j])>1){
       g[i].push_back(j);
       g[j].push_back(i);
   }
  }
 }
 for(int i=0;i<n;i++)vis[i]=0,maxfrom[i]=-1;
 for(int i=0;i<n;i++){
  if(vis[i])continue;
  bfs(i);
 }
 Solve();
 return 0;   
}

Compiler output:
a.cc: In function 'void Solve()':
a.cc:26:24: error: no matching function for call to 'std::priority_queue<long long int>::push(std::pair<long long int, int>)'
   26 |      if(nz[i]==0)Q.push(make_pair(a[i],i));
      |                  ~~~~~~^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/queue:66,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:157,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_queue.h:736:7: note: candidate: 'void std::priority_queue<_Tp, _Sequence, _Compare>::push(const value_type&) [with _Tp = long long int; _Sequence = std::vector<long long int>; _Compare = std::less<long long int>; value_type = long long int]'
  736 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:736:30: note:   no known conversion for argument 1 from 'std::pair<long long int, int>' to 'const std::priority_queue<long long int>::value_type&' {aka 'const long long int&'}
  736 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:744:7: note: candidate: 'void std::priority_queue<_Tp, _Sequence, _Compare>::push(value_type&&) [with _Tp = long long int; _Sequence = std::vector<long long int>; _Compare = std::less<long long int>; value_type = long long int]'
  744 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:744:25: note:   no known conversion for argument 1 from 'std::pair<long long int, int>' to 'std::priority_queue<long long int>::value_type&&' {aka 'long long int&&'}
  744 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~
a.cc:29:16: error: request for member 'second' in 'Q.std::priority_queue<long long int>::top()', which is of non-class type 'const __gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'const long long int'}
   29 |   ll x=Q.top().second;
      |                ^~~~~~
a.cc:34:22: error: no matching function for call to 'std::priority_queue<long long int>::push(std::pair<long long int, long long int>)'
   34 |    if(nz[y]==0)Q.push(make_pair(a[y],y));
      |                ~~~~~~^~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:736:7: note: candidate: 'void std::priority_queue<_Tp, _Sequence, _Compare>::push(const value_type&) [with _Tp = long long int; _Sequence = std::vector<long long int>; _Compare = std::less<long long int>; value_type = long long int]'
  736 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:736:30: note:   no known conversion for argument 1 from 'std::pair<long long int, long long int>' to 'const std::priority_queue<long long int>::value_type&' {aka 'const long long int&'}
  736 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:744:7: note: candidate: 'void std::priority_queue<_Tp, _Sequence, _Compare>::push(value_type&&) [with _Tp = long long int; _Sequence = std::vector<long long int>; _Compare = std::less<long long int>; value_type = long long int]'
  744 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:744:25: note:   no known conversion for argument 1 from 'std::pair<long long int, long long int>' to 'std::priority_queue<long long int>::value_type&&' {aka 'long long int&&'}
  744 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~
a.cc: In function 'void bfs(ll)':
a.cc:46:8: error: 'vis' was not declared in this scope
   46 |     if(vis[x])return; vis[x]=1;
      |        ^~~
a.cc:46:23: error: 'vis' was not declared in this scope
   46 |     if(vis[x])return; vis[x]=1;
      |                       ^~~
a.cc: In function 'int main()':
a.cc:66:22: error: 'vis' was not declared in this scope
   66 |  for(int i=0;i<n;i++)vis[i]=0,maxfrom[i]=-1;
      |                      ^~~
a.cc:68:6: error: 'vis' was not declared in this scope
   68 |   if(vis[i])continue;
      |      ^~~