結果
問題 |
No.1059 素敵な集合
|
ユーザー |
![]() |
提出日時 | 2020-06-06 15:46:55 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 109 ms / 2,000 ms |
コード長 | 1,589 bytes |
コンパイル時間 | 1,739 ms |
コンパイル使用メモリ | 176,932 KB |
実行使用メモリ | 69,556 KB |
最終ジャッジ日時 | 2024-07-23 09:39:49 |
合計ジャッジ時間 | 2,832 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<(int)(n); i++) using namespace std; using LL = long long; class UnionFind { public: vector <LL> par; vector <LL> siz; UnionFind(LL sz_): par(sz_), siz(sz_, 1LL) { for (LL i = 0; i < sz_; ++i) par[i] = i; } void init(LL sz_) { par.resize(sz_); siz.assign(sz_, 1LL); for (LL i = 0; i < sz_; ++i) par[i] = i; } LL root(LL x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool unite(LL x, LL y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool same(LL x, LL y) { return root(x) == root(y); } LL size(LL x) { return siz[root(x)]; } }; struct edge{ int from, to; LL cost; edge(int from, int to, LL cost): from(from), to(to), cost(cost) {} bool operator<(const edge &e) const{ return cost < e.cost; } }; vector<edge> es; LL kruskal(int siz){ sort(es.begin(),es.end()); UnionFind uf(siz); LL res=0; rep(i,es.size()){ edge e=es[i]; if(!uf.same(e.from, e.to)){ uf.unite(e.from, e.to); res+=e.cost; } } return res; } int main(){ int L, R; cin >> L >> R; for(int i=L; i<R; i++){ for(int j=2; i*j<=R; j++){ es.emplace_back(i,i*j,0); } es.emplace_back(i,i+1,1); } LL ans=kruskal(R+1); cout << ans << endl; return 0; }