結果

問題 No.826 連絡網
ユーザー Pachicobue
提出日時 2019-04-23 15:32:28
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 945 bytes
コンパイル時間 3,058 ms
コンパイル使用メモリ 107,796 KB
最終ジャッジ日時 2025-01-07 02:51:42
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26 TLE * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <vector>
#include <cassert>
int main()
{
    int N, P;
    std::cin >> N >> P;
    assert(1 <= N and N <= 1000000);
    assert(1 <= P and P <= N);
    std::vector<std::vector<int>> G(N + 1);
    std::vector<bool> isPrime(N + 1, true);
    isPrime[0] = isPrime[1] = true;
    for (int i = 2; i <= N; i++) {
        if (not isPrime[i]) { continue; }
        for (int j = 2; i * j <= N; j++) {
            G[i].push_back(i * j);
            G[i * j].push_back(i);
        }
    }
    std::vector<bool> visited(N + 1, false);
    std::queue<int> Q;
    Q.push(P);
    visited[P] = true;
    int ans = 1;
    while (not Q.empty()) {
        const int s = Q.front();
        Q.pop();
        for (const int to : G[s]) {
            if (visited[to]) { continue; }
            visited[to] = true;
            Q.push(to);
            ans++;
        }
    }
    std::cout << ans << std::endl;
    return 0;
}
0