結果

問題 No.1059 素敵な集合
ユーザー 👑 platinumplatinum
提出日時 2020-06-06 15:46:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 1,589 bytes
コンパイル時間 1,643 ms
コンパイル使用メモリ 173,548 KB
実行使用メモリ 70,612 KB
最終ジャッジ日時 2023-09-30 15:39:12
合計ジャッジ時間 3,107 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 117 ms
70,612 KB
testcase_02 AC 12 ms
8,860 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 10 ms
8,236 KB
testcase_07 AC 10 ms
7,488 KB
testcase_08 AC 13 ms
7,680 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 21 ms
13,312 KB
testcase_11 AC 11 ms
8,740 KB
testcase_12 AC 6 ms
5,048 KB
testcase_13 AC 21 ms
12,316 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 39 ms
20,168 KB
testcase_16 AC 12 ms
8,764 KB
testcase_17 AC 11 ms
7,752 KB
testcase_18 AC 9 ms
7,872 KB
testcase_19 AC 108 ms
69,476 KB
testcase_20 AC 104 ms
69,460 KB
testcase_21 AC 36 ms
19,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0