結果

問題 No.826 連絡網
ユーザー 🍮かんプリン
提出日時 2019-08-08 18:53:52
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,996 bytes
コンパイル時間 1,883 ms
コンパイル使用メモリ 164,976 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-07-18 20:49:09
合計ジャッジ時間 2,416 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void input(int&, R& ...) [with R = {int}]’:
main.cpp:15:60: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   15 | template<typename... R> void input(int& f, R&... r) { scanf("%d", &f); input(r...); }
      |                                                       ~~~~~^~~~~~~~~~
main.cpp: In function ‘void input(int&, R& ...) [with R = {}]’:
main.cpp:15:60: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]

ソースコード

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