結果

問題 No.1059 素敵な集合
ユーザー betrue12betrue12
提出日時 2020-05-22 21:36:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,013 bytes
コンパイル時間 1,944 ms
コンパイル使用メモリ 202,092 KB
実行使用メモリ 4,696 KB
最終ジャッジ日時 2023-09-30 15:25:34
合計ジャッジ時間 2,897 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 14 ms
4,696 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 7 ms
4,384 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 3 ms
4,604 KB
testcase_19 AC 14 ms
4,684 KB
testcase_20 AC 13 ms
4,684 KB
testcase_21 AC 7 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct UnionFind {
    vector<int> par;
    vector<int> sz;

    UnionFind(int n=0){ if(n>0) initialize(n); }
    void initialize(int n){
        par.resize(n);
        sz.assign(n, 1);
        for(int i=0; i<n; i++){
            par[i] = i;
        }
    }

    int find(int x){
        if(par[x] == x){
            return x;
        }else{
            return par[x] = find(par[x]);
        }
    }

    bool unite(int x, int y){
        x = find(x), y = find(y);
        if(x == y) return false;
        if(sz[x] > sz[y]) swap(x, y);
        par[x] = y;
        sz[y] += sz[x];
        return true;
    }

    bool same(int x, int y){ return find(x) == find(y); }
    int size(int x){ return sz[find(x)]; }
};

int main(){
    int L, R;
    cin >> L >> R;
    UnionFind uf(R+1);
    for(int i=L; i<=R; i++) for(int j=2*i; j<=R; j+=i) uf.unite(i, j);
    int ans = -1;
    for(int i=L; i<=R; i++) if(uf.find(i) == i) ans++;
    cout << ans << endl;
    return 0;
}
0