結果

問題 No.826 連絡網
ユーザー Y17Y17
提出日時 2019-05-03 22:08:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,066 bytes
コンパイル時間 1,416 ms
コンパイル使用メモリ 163,032 KB
実行使用メモリ 9,036 KB
最終ジャッジ日時 2024-05-03 05:10:17
合計ジャッジ時間 3,072 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 68 ms
7,296 KB
testcase_13 AC 24 ms
5,376 KB
testcase_14 AC 50 ms
6,272 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 32 ms
5,376 KB
testcase_17 AC 25 ms
5,376 KB
testcase_18 AC 17 ms
5,376 KB
testcase_19 AC 82 ms
8,064 KB
testcase_20 AC 77 ms
8,064 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 25 ms
5,376 KB
testcase_23 AC 33 ms
5,376 KB
testcase_24 AC 13 ms
5,376 KB
testcase_25 AC 97 ms
8,960 KB
testcase_26 AC 15 ms
5,376 KB
testcase_27 AC 69 ms
7,424 KB
testcase_28 AC 53 ms
6,400 KB
testcase_29 AC 23 ms
5,376 KB
testcase_30 AC 97 ms
9,036 KB
testcase_31 AC 29 ms
5,376 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