1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include<iostream> #include<cstdio> #include<algorithm> #include<queue> using namespace std; const int N=2e3+10,M=4e6+10; template<class T> inline void read(T &x) { x=0;bool f=0; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); if(f)x=~x+1; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } int n; int a[N]; bool g[N][N],vis[N]; int tot,head[N],ver[M<<1],ne[M<<1]; int deg[N];
int gcd(int x,int y){return x?gcd(y%x,x):y;} inline void add(int u,int v) { ver[++tot]=v; ne[tot]=head[u]; head[u]=tot; deg[v]++; } void dfs(int x) { vis[x]=1; for(int y=1;y<=n;y++) if(!vis[y]&&g[x][y]) dfs(y),add(x,y); } void topo() { priority_queue<int>q; for(int i=1;i<=n;i++)if(!deg[i])q.push(i); while(!q.empty()) { int x=q.top(); q.pop(); printf("%d ",a[x]); for(int i=head[x];i;i=ne[i]) { int y=ver[i]; if(!--deg[y])q.push(y); } } } int main() { read(n); for(int i=1;i<=n;i++)read(a[i]); sort(a+1,a+1+n); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) g[i][j]=g[j][i]=(gcd(a[i],a[j])!=1); for(int i=1;i<=n;i++) if(!vis[i]) dfs(i); topo(); return 0; }
|