結果
問題 |
No.1917 LCMST
|
ユーザー |
|
提出日時 | 2024-03-12 20:44:44 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 630 ms / 4,000 ms |
コード長 | 1,862 bytes |
コンパイル時間 | 2,058 ms |
コンパイル使用メモリ | 178,884 KB |
実行使用メモリ | 137,828 KB |
最終ジャッジ日時 | 2024-09-29 22:25:49 |
合計ジャッジ時間 | 25,798 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
ソースコード
// Make the best become better // No room for laziness #include<bits/stdc++.h> #define int long long #define pb push_back #define fi first #define se second using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int maxN = 1e6 + 5; const int mod = 1e9 + 7; const ll oo = 1e18; int n; int a[maxN]; vector<int> vc[maxN]; vector<int> L[maxN]; int lab[maxN]; int Findset(int u) { return lab[u] < 0 ? u : lab[u] = Findset(lab[u]); } bool Unite(int u, int v) { int r = Findset(u), s = Findset(v); if(r == s) return false; if(lab[r] > lab[s]) swap(r, s); lab[r] += lab[s]; lab[s] = r; return true; } struct TEdge { int u, v, w; }; vector<TEdge> e; void ReadInput() { cin >> n; for(int i=1; i<=n; i++) { cin >> a[i]; vc[a[i]].pb(i); } } void Solve() { for(int i=1; i<maxN; i++) { for(int j=1; j<vc[i].size(); j++) e.pb({vc[i][0], vc[i][j], i}); while(vc[i].size() > 1) vc[i].pop_back(); } for(int i=1; i<maxN; i++) { for(int j=i; j<maxN; j+=i) { for(int v : vc[j]) L[i].pb(v); } } for(int i=1; i<maxN; i++) { sort(L[i].begin(), L[i].end(), [](int i, int j) { return a[i] < a[j]; }); for(int j=1; j<L[i].size(); j++) e.pb({L[i][0], L[i][j], a[L[i][0]] * a[L[i][j]] / i}); } memset(lab, -1, sizeof lab); sort(e.begin(), e.end(), [](TEdge p, TEdge q) { return p.w < q.w; }); int res = 0; for(TEdge a : e) { if(Unite(a.u, a.v)) res += a.w; } cout << res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ReadInput(); Solve(); }