結果

問題 No.1917 LCMST
ユーザー Nguyên Bùi QuangNguyên Bùi Quang
提出日時 2023-11-02 20:16:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,427 bytes
コンパイル時間 3,048 ms
コンパイル使用メモリ 217,708 KB
実行使用メモリ 814,884 KB
最終ジャッジ日時 2023-11-02 20:16:57
合計ジャッジ時間 11,547 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
18,400 KB
testcase_01 AC 55 ms
18,400 KB
testcase_02 AC 62 ms
18,400 KB
testcase_03 AC 152 ms
26,760 KB
testcase_04 AC 152 ms
26,760 KB
testcase_05 AC 149 ms
26,760 KB
testcase_06 AC 148 ms
26,760 KB
testcase_07 AC 174 ms
26,760 KB
testcase_08 AC 58 ms
18,400 KB
testcase_09 MLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const long long MODULO = 1e9 + 7;

int N;
int A[1000001];
vector<int> indexes[100001];
vector<int> factors[100001];
long long result = 0;
bitset<1000001> visited;

int M;
vector<int> v;
vector<pair<long long, pair<int, int>>> edges;

namespace DSU
{
    vector<int> dsu, min;

    void init(const int& size)
    {
        dsu.clear(), dsu.resize(size + 1, -1);
        min.clear(), min.resize(size + 1);
        iota(min.begin(), min.end(), 0);
    }

    int find(const int& u)
    {
        return dsu[u] < 0 ? u : dsu[u] = find(dsu[u]);
    }

    bool unite(int u, int v)
    {
        u = find(u), v = find(v);
        if (u == v)
            return false;
        if (dsu[u] < dsu[v])
            swap(u, v);
        dsu[v] += dsu[u];
        dsu[u] = v;
        if (A[min[u]] < A[min[v]])
            min[v] = min[u];
        return true;
    }

    int getComponentMin(const int& u)
    {
        return min[find(u)];
    }
}

void KruskalsAlgorithm()
{
    sort(edges.begin(), edges.end());
    int mstEdgesCount = 0;
    DSU::init(M);
    for (auto &[weight, edge] : edges)
    {
        int i = edge.first, j = edge.second;
        if (DSU::unite(i, j))
        {
            result += weight;
            if (++mstEdgesCount == M - 1) return;
        }
    }
}

int main()
{
    // freopen("inp.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);

    ios::sync_with_stdio(false);

    for (int i = 1; i <= 100000; i++)
        for (int j = i; j <= 100000; j += i)
            factors[j].push_back(i);

    cin >> N;
    for (int i = 1; i <= N; i++)
        cin >> A[i];
    sort(A + 1, A + N + 1);
    for (int i = 1; i <= N; i++)
        indexes[A[i]].push_back(i);

    DSU::init(N);

    for (int i = 1; i <= N; i++)
        for (auto &f : factors[A[i]])
            if (!indexes[f].empty() && indexes[f].front() < i)
            {
                DSU::unite(indexes[f].front(), i);
                result += A[i];
                break;
            }

    for (int i = 1; i <= N; i++)
    {
        int u = DSU::find(i);
        if (visited[u]) continue;
        visited[u] = true;
        v.push_back(A[DSU::getComponentMin(u)]);
    }

    M = (int)v.size();
    for (int i = 0; i < M; i++)
        for (int j = 0; j < i; j++)
            edges.emplace_back(lcm(1ll * v[i], 1ll * v[j]), make_pair(i, j));

    KruskalsAlgorithm();

    cout << result;
}
0