結果

問題 No.826 連絡網
ユーザー PachicobuePachicobue
提出日時 2019-04-11 21:15:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 1,376 bytes
コンパイル時間 882 ms
コンパイル使用メモリ 75,904 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2024-05-03 05:01:17
合計ジャッジ時間 3,128 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 77 ms
14,336 KB
testcase_13 AC 26 ms
7,680 KB
testcase_14 AC 53 ms
11,648 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 35 ms
8,832 KB
testcase_17 AC 27 ms
7,680 KB
testcase_18 AC 21 ms
6,784 KB
testcase_19 AC 95 ms
16,384 KB
testcase_20 AC 93 ms
16,128 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 27 ms
7,808 KB
testcase_23 AC 36 ms
9,088 KB
testcase_24 AC 15 ms
5,888 KB
testcase_25 AC 122 ms
18,816 KB
testcase_26 AC 18 ms
6,144 KB
testcase_27 AC 79 ms
14,848 KB
testcase_28 AC 58 ms
12,160 KB
testcase_29 AC 28 ms
7,680 KB
testcase_30 AC 125 ms
18,944 KB
testcase_31 AC 33 ms
8,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <numeric>
#include <vector>
class UnionFind
{
public:
    UnionFind(const std::size_t v) : V(v), parent(v), size(v, 1) { std::iota(parent.begin(), parent.end(), 0); }
    std::size_t find(const std::size_t a) { return assert(a < V), parent[a] == a ? a : parent[a] = find(parent[a]); }
    bool same(const std::size_t a, const std::size_t b)
    {
        assert(a < V), assert(b < V);
        return find(a) == find(b);
    }
    void unite(std::size_t a, std::size_t b)
    {
        assert(a < V), assert(b < V);
        a = find(a), b = find(b);
        if (a == b) { return; }
        if (size[a] < size[b]) { std::swap(a, b); }
        size[a] += size[b], parent[b] = a;
    }
    std::size_t getSize(const std::size_t a) { return assert(a < V), size[find(a)]; }
    void debugPrint() const
    {
        std::cerr << "[";
        for (std::size_t i = 0; i < parent.size(); i++) { std::cerr << parent[i] << (i + 1 == parent.size() ? "" : ","); }
        std::cerr << "]\n";
    }

private:
    const std::size_t V;
    std::vector<std::size_t> parent, size;
};
int main()
{
    std::size_t N, P;
    std::cin >> N >> P;
    UnionFind uf(N + 1);
    for (std::size_t i = 2; i <= N; i++) {
        for (std::size_t j = i; j <= N; j += i) { uf.unite(i, j); }
    }
    std::cout << uf.getSize(P) << std::endl;
    return 0;
}
0