結果

問題 No.826 連絡網
ユーザー misora192misora192
提出日時 2020-07-23 22:52:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 1,588 bytes
コンパイル時間 1,631 ms
コンパイル使用メモリ 170,304 KB
実行使用メモリ 15,024 KB
最終ジャッジ日時 2023-09-06 02:56:05
合計ジャッジ時間 3,866 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 27 ms
11,620 KB
testcase_13 AC 11 ms
6,484 KB
testcase_14 AC 20 ms
9,360 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 14 ms
7,264 KB
testcase_17 AC 11 ms
6,404 KB
testcase_18 AC 8 ms
5,748 KB
testcase_19 AC 31 ms
13,144 KB
testcase_20 AC 30 ms
12,984 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 11 ms
6,400 KB
testcase_23 AC 14 ms
7,548 KB
testcase_24 AC 6 ms
4,824 KB
testcase_25 AC 38 ms
15,016 KB
testcase_26 AC 8 ms
5,344 KB
testcase_27 AC 27 ms
11,884 KB
testcase_28 AC 22 ms
9,968 KB
testcase_29 AC 11 ms
6,508 KB
testcase_30 AC 38 ms
15,024 KB
testcase_31 AC 13 ms
7,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=(0);i<(n);i++)

using namespace std;

typedef long long ll;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

// union-find tree with size(struct)
struct UnionFind{
    vector<int> par; // 親ノード
    vector<int> rank; // ランク
	vector<int> size; // 連結成分のサイズ

    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n);
		rank.resize(n);
		size.resize(n);
        for (int i = 0; i < n; ++i){
			par[i] = i;
			rank[i] = 0;
			size[i] = 1;
		}
    }

    int find(int x) {
        if (par[x] == x)  return x;

        int r = find(par[x]);
        return par[x] = r;
    }

    bool issame(int x, int y) {
        return find(x) == find(y);
    }

    bool unite(int x, int y) {
	    x = find(x);
		y = find(y);
	    if (x == y) return false;

	    if (rank[x] < rank[y]) swap(x, y);
	    if (rank[x] == rank[y]) ++rank[x];
	    par[y] = x;
		size[x] += size[y];
	    return true;
    }

	int getSize(int x){
		return size[find(x)];
	}

	int getRank(int x){
		return rank[find(x)];
	}
};


int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, p;
	cin >> n >> p;

	UnionFind uf(n+1);
	vector<bool> isprime(n+1, true);
	isprime[0] = isprime[1] = false;
	for(int i = 2; i <= n; i++){
		if(isprime[i]){
			for(int j = 2 * i; j <= n; j += i){
				uf.unite(i, j);
				isprime[j] = false;
			}
		}
	}

	cout << uf.getSize(p) << endl;
}
0