結果
問題 |
No.1917 LCMST
|
ユーザー |
|
提出日時 | 2023-02-11 18:17:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 701 ms / 4,000 ms |
コード長 | 1,987 bytes |
コンパイル時間 | 2,237 ms |
コンパイル使用メモリ | 206,944 KB |
最終ジャッジ日時 | 2025-02-10 14:28:49 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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]] / __gcd(a[L[i][0]], a[L[i][j]])}); } 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) { //cout << a.u << " " << a.v << " " << a.w << '\n'; if(Unite(a.u, a.v)) res += a.w; } cout << res; } int32_t main() { //freopen("sol.inp", "r", stdin); ios_base::sync_with_stdio(false); cin.tie(nullptr); ReadInput(); Solve(); }