結果

問題 No.826 連絡網
ユーザー face4face4
提出日時 2019-05-03 21:30:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,487 bytes
コンパイル時間 735 ms
コンパイル使用メモリ 72,968 KB
実行使用メモリ 9,480 KB
最終ジャッジ日時 2023-08-30 05:57:05
合計ジャッジ時間 16,432 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
9,480 KB
testcase_01 AC 12 ms
4,984 KB
testcase_02 AC 132 ms
5,156 KB
testcase_03 AC 1,523 ms
5,032 KB
testcase_04 TLE -
testcase_05 AC 823 ms
5,152 KB
testcase_06 AC 836 ms
5,100 KB
testcase_07 AC 1,844 ms
5,172 KB
testcase_08 AC 949 ms
5,052 KB
testcase_09 AC 1,997 ms
5,104 KB
testcase_10 AC 464 ms
5,136 KB
testcase_11 AC 1,361 ms
5,164 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

class DisjointSet{
  public:
    vector<int> rank, p;

    DisjointSet(){}
    DisjointSet(int size){
        rank.resize(size, 0);
        p.resize(size, 0);
        for(int i = 0; i < size; i++) makeSet(i);
    }

    void makeSet(int x){
        p[x] = x;
        rank[x] = 0;
    }

    bool same(int x, int y){
        return findSet(x) == findSet(y);
    }
    
    void unite(int x, int y){
        link(findSet(x), findSet(y));
    }

    void link(int x, int y){
        if(rank[x] > rank[y]){
            p[y] = x;
        }else{
            p[x] = y;
            if(rank[x] == rank[y]){
                rank[y]++;
            }
        }
    }

    int findSet(int x){
        if(x != p[x]){
            // path compression
            p[x] = findSet(p[x]);
        }
        return p[x];
    }
};

int main(){
    int n, p;
    cin >> n >> p;
    vector<int> prime;
    bool nonp[1000001] = {};
    for(int i = 2; i < 1000001; i++){
        if(nonp[i]) continue;
        prime.push_back(i);
        for(int j = i+i; j < 1000001; j+= i)    nonp[j] = true;
    }
    int pnum = prime.size();
    DisjointSet uf(n + pnum);
    // ここが重いかもしれない
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < pnum; j++){
            if(i%prime[j] == 0) uf.unite(i-1, n+j);
        }
    }
    int ans = 0;
    for(int i = 0; i < n; i++)  if(uf.same(p-1,i))  ans++;
    cout << ans << endl;
    return 0;
}
0