結果

問題 No.826 連絡網
ユーザー tokizo
提出日時 2019-05-03 23:43:14
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,140 bytes
コンパイル時間 1,980 ms
コンパイル使用メモリ 170,456 KB
実行使用メモリ 7,268 KB
最終ジャッジ日時 2024-11-24 02:33:13
合計ジャッジ時間 2,635 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct UnionFind{
    vector<int> par;
    UnionFind(int N) : par(N){
        for(int i = 0; i < N; i++){
            par[i] = -1;
        }
    }
    int root(int x){
        if(par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    void unite(int x, int y){
        int rx = root(x);
        int ry = root(y);
        if(rx == ry) return;
        else{
            if(par[rx] > par[ry]) swap(rx, ry);
            par[rx] += par[ry];
            par[ry] = rx;
       }
    }
    bool same(int x, int y){
        return root(x) == root(y);
    }
    void check(){
        for(auto x : par){
            cout << x << " ";
        }
        cout << endl;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n, p;
    cin >> n >> p;

    UnionFind uf(n + 1);

    vector<bool> F(n + 1, true);
    for(int i = 2; i <= n; i++){
        if(F[i]){
            for(int j = i + i; j <= n; j += i){
                uf.unite(i, j);
                F[j] = false;
            }
        }
    }

    cout << -uf.par[uf.root(p)] << endl;

    return 0;
}
0