結果

問題 No.826 連絡網
ユーザー Y17Y17
提出日時 2019-05-03 22:08:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 1,066 bytes
コンパイル時間 1,968 ms
コンパイル使用メモリ 147,608 KB
実行使用メモリ 9,036 KB
最終ジャッジ日時 2023-08-15 18:19:43
合計ジャッジ時間 3,880 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 71 ms
7,384 KB
testcase_13 AC 23 ms
4,892 KB
testcase_14 AC 52 ms
6,300 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 32 ms
5,396 KB
testcase_17 AC 24 ms
4,956 KB
testcase_18 AC 17 ms
4,540 KB
testcase_19 AC 84 ms
7,956 KB
testcase_20 AC 81 ms
7,940 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 25 ms
4,760 KB
testcase_23 AC 33 ms
5,472 KB
testcase_24 AC 12 ms
4,380 KB
testcase_25 AC 104 ms
9,036 KB
testcase_26 AC 16 ms
4,420 KB
testcase_27 AC 73 ms
7,496 KB
testcase_28 AC 54 ms
6,504 KB
testcase_29 AC 24 ms
4,728 KB
testcase_30 AC 104 ms
9,016 KB
testcase_31 AC 30 ms
5,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class UnionFind{
public:
    vector<int> tree;

    void init(){
        for(int i = 0;i < tree.size();i++){
            tree[i] = -1;
        }
        return;
    }

    UnionFind(int n){
        n++;
        tree.resize(n);
        init();
        return;
    }

    int root(int i){
        if(tree[i] < 0) return i;
        return tree[i] = root(tree[i]);
    }

    bool unite(int i, int j){
        i = root(i);
        j = root(j);
        if(i == j) return false;

        tree[i] += tree[j];
        tree[j] = i;
        return true;
    }

    int size(int i){
        return -tree[root(i)];
    }

    bool same(int i, int j){
        return root(i) == root(j);
    }
};

int used[1000010] = {};

int main(){
    int n, p;
    cin >> n >> p;

    UnionFind tree(n);

    for(int i = 2;i <= n;i++){
        if(!used[i]){
            for(int j = i*2;j <= n;j+=i){
                tree.unite(i, j);
                used[i] = 1;
            }
        }
    }

    cout << tree.size(p) << endl;

    return 0;
}
0