結果

問題 No.1917 LCMST
ユーザー ei1333333ei1333333
提出日時 2022-04-29 23:05:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 578 ms / 4,000 ms
コード長 3,792 bytes
コンパイル時間 5,123 ms
コンパイル使用メモリ 267,912 KB
実行使用メモリ 148,712 KB
最終ジャッジ日時 2023-09-11 14:34:04
合計ジャッジ時間 22,070 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
5,492 KB
testcase_01 AC 5 ms
5,344 KB
testcase_02 AC 6 ms
5,420 KB
testcase_03 AC 9 ms
6,048 KB
testcase_04 AC 9 ms
6,052 KB
testcase_05 AC 10 ms
6,128 KB
testcase_06 AC 9 ms
6,020 KB
testcase_07 AC 10 ms
6,044 KB
testcase_08 AC 6 ms
5,416 KB
testcase_09 AC 548 ms
147,096 KB
testcase_10 AC 552 ms
146,580 KB
testcase_11 AC 543 ms
145,788 KB
testcase_12 AC 504 ms
139,520 KB
testcase_13 AC 293 ms
114,440 KB
testcase_14 AC 516 ms
142,808 KB
testcase_15 AC 243 ms
105,244 KB
testcase_16 AC 289 ms
115,968 KB
testcase_17 AC 371 ms
124,764 KB
testcase_18 AC 412 ms
130,104 KB
testcase_19 AC 288 ms
104,064 KB
testcase_20 AC 240 ms
100,216 KB
testcase_21 AC 268 ms
104,224 KB
testcase_22 AC 238 ms
101,208 KB
testcase_23 AC 255 ms
100,384 KB
testcase_24 AC 281 ms
105,000 KB
testcase_25 AC 260 ms
101,868 KB
testcase_26 AC 254 ms
103,380 KB
testcase_27 AC 281 ms
104,824 KB
testcase_28 AC 290 ms
104,836 KB
testcase_29 AC 575 ms
147,972 KB
testcase_30 AC 575 ms
147,728 KB
testcase_31 AC 578 ms
148,684 KB
testcase_32 AC 569 ms
147,060 KB
testcase_33 AC 574 ms
147,056 KB
testcase_34 AC 176 ms
91,364 KB
testcase_35 AC 175 ms
91,760 KB
testcase_36 AC 176 ms
91,572 KB
testcase_37 AC 569 ms
147,388 KB
testcase_38 AC 549 ms
148,712 KB
testcase_39 AC 553 ms
147,808 KB
testcase_40 AC 558 ms
147,176 KB
testcase_41 AC 546 ms
147,400 KB
testcase_42 AC 552 ms
148,152 KB
testcase_43 AC 261 ms
91,780 KB
testcase_44 AC 263 ms
92,332 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(2);
    }
  }
  cout << kruskal(es, N).cost << "\n";
}
0