結果

問題 No.826 連絡網
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2019-08-08 18:53:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,996 bytes
コンパイル時間 1,423 ms
コンパイル使用メモリ 151,120 KB
実行使用メモリ 7,072 KB
最終ジャッジ日時 2023-09-26 01:23:09
合計ジャッジ時間 3,509 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 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,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 19 ms
5,896 KB
testcase_13 AC 7 ms
4,376 KB
testcase_14 AC 14 ms
5,220 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 9 ms
4,728 KB
testcase_17 AC 8 ms
4,376 KB
testcase_18 AC 6 ms
4,376 KB
testcase_19 AC 21 ms
6,608 KB
testcase_20 AC 20 ms
6,536 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 9 ms
4,380 KB
testcase_23 AC 10 ms
4,688 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 AC 24 ms
7,068 KB
testcase_26 AC 6 ms
4,376 KB
testcase_27 AC 18 ms
6,300 KB
testcase_28 AC 15 ms
5,440 KB
testcase_29 AC 8 ms
4,376 KB
testcase_30 AC 25 ms
7,072 KB
testcase_31 AC 9 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region include
#include "bits/stdc++.h"
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define REPR(i, n) for(int i = (int)(n); i >= 0; i--)
#define FOR(i,n,m) for(int i = (int)(n); i < int(m); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 6;
const ll LLINF = 4e18;
void input() {}
template<typename... R> void input(int& f, R&... r) { scanf("%d", &f); input(r...); }
template<typename... R> void input(double& f, R&... r) { scanf("%lf", &f); input(r...); }
template<typename... R> void input(ll& f, R&... r) { scanf("%lld", &f); input(r...); }
template<typename... R> void input(char& f, R&... r) { scanf("%c", &f); input(r...); }
template<typename T, typename... R> void input(vector<T>& f, R&... r) { REP(i, f.size())input(f[i]); input(r...); }
#pragma endregion

//UnionFind
class UnionFind {
private:
    vector<int> par;
public:
    UnionFind(int n) {
        par.resize(n, -1);
    }

    int root(int x) {
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }

    bool unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return false;
        if (size(rx) < size(ry)) swap(rx, ry);
        par[rx] += par[ry];
        par[ry] = rx;
        return true;
    }

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

    int size(int x) {
        return -par[root(x)];
    }
};

int main() {
    int N, P; input(N,P);
    vector<bool> used(N, false);
    UnionFind uni(N);
    for(int i = 2; i<=N;i++) {
        if (!used[i - 1]) {
            used[i - 1] = true;
            for (int j = i + i; j <= N; j += i) {
                used[j - 1] = true;
                uni.unite(i - 1, j - 1);
            }
        }
    }
    cout << uni.size(P-1) << endl;
    getchar(); getchar();
}
0