Submission #1108513


Source Code Expand

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr<< #x <<": "<<x<<endl
#define debug2(x,y) cerr<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;
template<class T> using vv=vector<vector< T > >;

vi a; // number of node
vvi g;
vvi child;
set<int, greater<int> > root;
deque<bool> reached;

void dfs(int s) {
  root.insert(s);
  deque<int> st;
  st.push_back(s);
  reached[s] = true;
  while (!st.empty()) {
    int u = st.back();
    for (int v : g[u]) {
      if (!reached[v]) {
        child[u].push_back(v);
        st.push_back(v);
        reached[v] = true;
        st.push_back(v);
        break;
      }
    }
    if (st.back() == u) {
      st.pop_back();
    }
  }
}

vi retrieve(int r) {
  int sz = (int)child[r].size();
  if (sz == 0) {
    return (vi){r};
  }
  int ret_size = 0;
  vvi tmp(sz);
  rep (i, sz) {
    tmp[i] = retrieve(child[r][i]);
    tmp[i].push_back(-1);
    ret_size += (int)tmp[i].size();
  }
  vi ret;
  ret.reserve(ret_size + 10);
  ret.push_back(r);
  vi index(sz, 0);
  while (true) {
    int next_i = -1;
    int selected = -1;
    rep (i, sz) {
      if (next_i < tmp[i][index[i]]) {
        next_i = tmp[i][index[i]];
        selected = i;
      }
    }
    if (next_i == -1) {
      break;
    }
    ret.push_back(next_i);
    index[selected] += 1;
  }
  return ret;
}

int main() {
  int n;
  scanf("%d", &n);
  a.resize(n);
  rep (i, n) {
    scanf("%d", &a[i]);
  }
  sort(all(a));

  g.resize(n);
  rep (i, n) {
    rep (j, n) {
      if (i == j) {
        continue;
      }
      if (__gcd(a[i], a[j]) == 1) {
        continue;
      }
      g[i].push_back(j);
      g[j].push_back(i);
    }
  }
  rep (i, n) {
    sort(all(g[i]));
  }

  // construct tree
  child.resize(n);
  reached.assign(n, false);
  rep (i, n) {
    if (reached[i]) {
      continue;
    }
    if (a[i] == 1) {
      root.insert(i);
      reached[i] = true;
      continue;
    }
    dfs(i);
  }

  for (int r : root) {
    vi tmp = retrieve(r);
    for (int i : tmp) {
      printf("%d ", a[i]);
    }
  }
  printf("\n");

  return 0;
}

Submission Info

Submission Time
Task E - Rearranging
User tspcx
Language C++14 (Clang 3.8.0)
Score 1600
Code Size 2870 Byte
Status AC
Exec Time 607 ms
Memory 31872 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1600 / 1600
Status
AC × 2
AC × 47
Set Name Test Cases
Sample sample1.txt, sample2.txt
All sample1.txt, sample2.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in41.txt, in42.txt, in43.txt, in44.txt, in45.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt
Case Name Status Exec Time Memory
in1.txt AC 602 ms 18560 KB
in10.txt AC 607 ms 19840 KB
in11.txt AC 447 ms 2944 KB
in12.txt AC 450 ms 2944 KB
in13.txt AC 446 ms 2816 KB
in14.txt AC 452 ms 2816 KB
in15.txt AC 448 ms 2816 KB
in16.txt AC 599 ms 31744 KB
in17.txt AC 599 ms 31872 KB
in18.txt AC 596 ms 31488 KB
in19.txt AC 599 ms 31488 KB
in2.txt AC 603 ms 18304 KB
in20.txt AC 596 ms 31488 KB
in21.txt AC 328 ms 512 KB
in22.txt AC 329 ms 512 KB
in23.txt AC 331 ms 512 KB
in24.txt AC 329 ms 512 KB
in25.txt AC 328 ms 512 KB
in26.txt AC 448 ms 2944 KB
in27.txt AC 449 ms 2944 KB
in28.txt AC 446 ms 2944 KB
in29.txt AC 450 ms 2944 KB
in3.txt AC 599 ms 18176 KB
in30.txt AC 449 ms 2944 KB
in31.txt AC 3 ms 256 KB
in32.txt AC 3 ms 256 KB
in33.txt AC 3 ms 256 KB
in34.txt AC 385 ms 512 KB
in35.txt AC 384 ms 512 KB
in36.txt AC 483 ms 5888 KB
in37.txt AC 484 ms 6016 KB
in38.txt AC 484 ms 5888 KB
in39.txt AC 485 ms 6016 KB
in4.txt AC 600 ms 18304 KB
in40.txt AC 482 ms 5760 KB
in41.txt AC 483 ms 5888 KB
in42.txt AC 484 ms 5760 KB
in43.txt AC 483 ms 5888 KB
in44.txt AC 484 ms 5632 KB
in45.txt AC 485 ms 5760 KB
in5.txt AC 599 ms 17664 KB
in6.txt AC 606 ms 19456 KB
in7.txt AC 605 ms 19456 KB
in8.txt AC 606 ms 18944 KB
in9.txt AC 600 ms 17920 KB
sample1.txt AC 3 ms 256 KB
sample2.txt AC 3 ms 256 KB