結果

問題 No.826 連絡網
ユーザー Y17
提出日時 2019-05-03 22:08:09
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,066 bytes
コンパイル時間 1,302 ms
コンパイル使用メモリ 161,144 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2024-11-24 02:24:24
合計ジャッジ時間 3,376 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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