結果

問題 No.826 連絡網
ユーザー tmsausatmsausa
提出日時 2019-05-03 21:50:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 2,222 bytes
コンパイル時間 1,901 ms
コンパイル使用メモリ 172,724 KB
実行使用メモリ 12,168 KB
最終ジャッジ日時 2023-08-15 18:16:00
合計ジャッジ時間 2,951 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 32 ms
11,696 KB
testcase_13 AC 13 ms
6,232 KB
testcase_14 AC 25 ms
10,772 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 17 ms
6,712 KB
testcase_17 AC 13 ms
6,228 KB
testcase_18 AC 11 ms
5,056 KB
testcase_19 AC 36 ms
11,060 KB
testcase_20 AC 37 ms
12,168 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 14 ms
6,408 KB
testcase_23 AC 17 ms
6,756 KB
testcase_24 AC 8 ms
4,748 KB
testcase_25 AC 45 ms
11,764 KB
testcase_26 AC 9 ms
4,932 KB
testcase_27 AC 33 ms
11,108 KB
testcase_28 AC 26 ms
10,388 KB
testcase_29 AC 13 ms
6,400 KB
testcase_30 AC 43 ms
11,388 KB
testcase_31 AC 15 ms
6,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define range(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define reversed_range(i, n) for (int (i) = (n) - 1; (i) >= 0; (i)--)
using namespace std;
template <typename T>
using vec = vector<T>;
using lint = long long;
using ulint = unsigned long long;

struct UnionFind {
private:
    vector<int> parent;
    vector<int> num_components;

public:
    UnionFind(int n) {
        num_components.resize(n, 1);
        for (int i = 0; i < n; i++) {
            parent.emplace_back(i);
        }
    }

    int root(int x) {
        if (parent.at(x) == x) return x;
        parent.at(x) = root(parent.at(x));
        return parent.at(x);
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }

    int num_connected(int x) {
        return num_components.at(x);
    }

    void unite(int x, int y) {
        int root_x = root(x);
        int root_y = root(y);
        if (root_x == root_y) return;
        int rank_x = num_components.at(root_x);
        int rank_y = num_components.at(root_y);
        if (rank_x < rank_y) {
            parent.at(root_x) = root_y;
            num_components.at(root_y) += rank_x;
        }
        else {
            parent.at(root_y) = root_x;
            num_components.at(root_x) += rank_y;
        }
    }
};

template <typename T>
ostream& operator <<(ostream& os, vec<T> v) {
    os << "[";
    for (int i = 0; i < v.size() - 1; i++) {
        os << v.at(i) << ", ";
    }
    return os << v.at(v.size() - 1) << "]";
}

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

vec<bool> sieve(int n) {
    vec<bool> is_prime(n + 1, true);
    is_prime.at(0) = false;
    is_prime.at(1) = false;
    for (int i = 2; i <= n; i++) {
        if (!is_prime.at(i)) continue;
        for (int j = 2 * i; j <= n; j += i) {
            is_prime.at(j) = false;
        }
    }
    return is_prime;
}

int main() {
    int N, P;
    cin >> N >> P;
    vec<bool> is_prime = sieve(N);
    UnionFind uf(N + 1);
    for (int i = 2; i <= N; i++) {
        if (is_prime.at(i)) {
            for (int j = i; j <= N; j += i) {
                uf.unite(i, j);
            }
        }
    }
    cout << uf.num_connected(uf.root(P)) << "\n";
}
0