結果

問題 No.1917 LCMST
ユーザー ei1333333ei1333333
提出日時 2022-04-29 23:07:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 466 ms / 4,000 ms
コード長 3,792 bytes
コンパイル時間 4,425 ms
コンパイル使用メモリ 267,244 KB
実行使用メモリ 122,364 KB
最終ジャッジ日時 2023-09-11 14:36:10
合計ジャッジ時間 19,651 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,400 KB
testcase_01 AC 6 ms
5,404 KB
testcase_02 AC 5 ms
5,448 KB
testcase_03 AC 9 ms
6,108 KB
testcase_04 AC 9 ms
6,096 KB
testcase_05 AC 10 ms
6,100 KB
testcase_06 AC 10 ms
6,108 KB
testcase_07 AC 9 ms
6,172 KB
testcase_08 AC 6 ms
5,456 KB
testcase_09 AC 416 ms
120,956 KB
testcase_10 AC 411 ms
122,076 KB
testcase_11 AC 423 ms
121,572 KB
testcase_12 AC 404 ms
118,304 KB
testcase_13 AC 256 ms
105,520 KB
testcase_14 AC 427 ms
119,796 KB
testcase_15 AC 226 ms
99,920 KB
testcase_16 AC 264 ms
106,404 KB
testcase_17 AC 302 ms
109,888 KB
testcase_18 AC 373 ms
112,828 KB
testcase_19 AC 247 ms
99,880 KB
testcase_20 AC 252 ms
98,664 KB
testcase_21 AC 271 ms
101,224 KB
testcase_22 AC 240 ms
98,704 KB
testcase_23 AC 249 ms
97,212 KB
testcase_24 AC 269 ms
99,956 KB
testcase_25 AC 248 ms
98,180 KB
testcase_26 AC 244 ms
100,316 KB
testcase_27 AC 259 ms
99,920 KB
testcase_28 AC 269 ms
99,872 KB
testcase_29 AC 466 ms
121,836 KB
testcase_30 AC 458 ms
121,860 KB
testcase_31 AC 448 ms
121,888 KB
testcase_32 AC 441 ms
121,668 KB
testcase_33 AC 448 ms
122,116 KB
testcase_34 AC 172 ms
91,740 KB
testcase_35 AC 170 ms
91,652 KB
testcase_36 AC 173 ms
92,128 KB
testcase_37 AC 448 ms
122,052 KB
testcase_38 AC 435 ms
122,228 KB
testcase_39 AC 442 ms
122,112 KB
testcase_40 AC 451 ms
121,628 KB
testcase_41 AC 441 ms
122,212 KB
testcase_42 AC 450 ms
122,364 KB
testcase_43 AC 262 ms
91,824 KB
testcase_44 AC 264 ms
92,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>

using namespace std;

using int64 = long long;
const int inf = (1 << 30) - 1;

#line 2 "graph/mst/kruskal.hpp"

#line 2 "graph/graph-template.hpp"

/**
 * @brief Graph Template(グラフテンプレート)
 */
template< typename T = int >
struct Edge {
  int from, to;
  T cost;
  int idx;

  Edge() = default;

  Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}

  operator int() const { return to; }
};

template< typename T = int >
struct Graph {
  vector< vector< Edge< T > > > g;
  int es;

  Graph() = default;

  explicit Graph(int n) : g(n), es(0) {}

  size_t size() const {
    return g.size();
  }

  void add_directed_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es++);
  }

  void add_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es);
    g[to].emplace_back(to, from, cost, es++);
  }

  void read(int M, int padding = -1, bool weighted = false, bool directed = false) {
    for(int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      a += padding;
      b += padding;
      T c = T(1);
      if(weighted) cin >> c;
      if(directed) add_directed_edge(a, b, c);
      else add_edge(a, b, c);
    }
  }

  inline vector< Edge< T > > &operator[](const int &k) {
    return g[k];
  }

  inline const vector< Edge< T > > &operator[](const int &k) const {
    return g[k];
  }
};

template< typename T = int >
using Edges = vector< Edge< T > >;
#line 1 "structure/union-find/union-find.cpp"

/**
 * @brief Union-Find
 * @docs docs/union-find.md
 */
struct UnionFind {
  vector< int > data;

  UnionFind() = default;

  explicit UnionFind(size_t sz) : data(sz, -1) {}

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return false;
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }

  int find(int k) {
    if(data[k] < 0) return (k);
    return data[k] = find(data[k]);
  }

  int size(int k) {
    return -data[find(k)];
  }

  bool same(int x, int y) {
    return find(x) == find(y);
  }

  vector< vector< int > > groups() {
    int n = (int) data.size();
    vector< vector< int > > ret(n);
    for(int i = 0; i < n; i++) {
      ret[find(i)].emplace_back(i);
    }
    ret.erase(remove_if(begin(ret), end(ret), [&](const vector< int > &v) {
      return v.empty();
    }));
    return ret;
  }
};

#line 5 "graph/mst/kruskal.hpp"

/**
 * @brief Kruskal(最小全域木)
 * @docs docs/kruskal.md
 */
template< typename T >
struct MinimumSpanningTree {
  T cost;
  Edges< T > edges;
};

template< typename T >
MinimumSpanningTree< T > kruskal(Edges< T > &edges, int V) {
  sort(begin(edges), end(edges), [](const Edge< T > &a, const Edge< T > &b) {
    return a.cost < b.cost;
  });
  UnionFind tree(V);
  T total = T();
  Edges< T > es;
  for(auto &e: edges) {
    if(tree.unite(e.from, e.to)) {
      es.emplace_back(e);
      total += e.cost;
    }
  }
  return {total, es};
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector< int > A(N);
  for(auto &a: A) cin >> a;

  vector< vector< int > > v(100001);
  for(int i = 0; i < N; i++) {
    v[A[i]].emplace_back(i);
  }

  vector< Edge< int64 > > es;
  vector< int > eval(N);
  for(int i = 100000; i >= 1; i--) {
    int low = inf, id = -1;
    for(int j = i; j <= 100000; j += i) {
      if(not v[j].empty()) {
        low = j;
        id = v[j].front();
        break;
      }
    }
    if(low == inf) continue;
    for(int j = i; j <= 100000; j += i) {
      for(auto &p: v[j]) {
        es.emplace_back(id, p, 1LL * low * j / i);
      }
      if(v[j].size() >= 2) v[j].resize(1);
    }
  }
  cout << kruskal(es, N).cost << "\n";
}
0