結果
| 問題 |
No.826 連絡網
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-05-03 21:50:16 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 48 ms / 2,000 ms |
| コード長 | 2,222 bytes |
| コンパイル時間 | 1,631 ms |
| コンパイル使用メモリ | 174,416 KB |
| 実行使用メモリ | 11,668 KB |
| 最終ジャッジ日時 | 2024-11-24 02:20:37 |
| 合計ジャッジ時間 | 2,951 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 30 |
ソースコード
#include <bits/stdc++.h>
#define range(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define reversed_range(i, n) for (int (i) = (n) - 1; (i) >= 0; (i)--)
using namespace std;
template <typename T>
using vec = vector<T>;
using lint = long long;
using ulint = unsigned long long;
struct UnionFind {
private:
vector<int> parent;
vector<int> num_components;
public:
UnionFind(int n) {
num_components.resize(n, 1);
for (int i = 0; i < n; i++) {
parent.emplace_back(i);
}
}
int root(int x) {
if (parent.at(x) == x) return x;
parent.at(x) = root(parent.at(x));
return parent.at(x);
}
bool same(int x, int y) {
return root(x) == root(y);
}
int num_connected(int x) {
return num_components.at(x);
}
void unite(int x, int y) {
int root_x = root(x);
int root_y = root(y);
if (root_x == root_y) return;
int rank_x = num_components.at(root_x);
int rank_y = num_components.at(root_y);
if (rank_x < rank_y) {
parent.at(root_x) = root_y;
num_components.at(root_y) += rank_x;
}
else {
parent.at(root_y) = root_x;
num_components.at(root_x) += rank_y;
}
}
};
template <typename T>
ostream& operator <<(ostream& os, vec<T> v) {
os << "[";
for (int i = 0; i < v.size() - 1; i++) {
os << v.at(i) << ", ";
}
return os << v.at(v.size() - 1) << "]";
}
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
vec<bool> sieve(int n) {
vec<bool> is_prime(n + 1, true);
is_prime.at(0) = false;
is_prime.at(1) = false;
for (int i = 2; i <= n; i++) {
if (!is_prime.at(i)) continue;
for (int j = 2 * i; j <= n; j += i) {
is_prime.at(j) = false;
}
}
return is_prime;
}
int main() {
int N, P;
cin >> N >> P;
vec<bool> is_prime = sieve(N);
UnionFind uf(N + 1);
for (int i = 2; i <= N; i++) {
if (is_prime.at(i)) {
for (int j = i; j <= N; j += i) {
uf.unite(i, j);
}
}
}
cout << uf.num_connected(uf.root(P)) << "\n";
}