結果
問題 | No.826 連絡網 |
ユーザー |
![]() |
提出日時 | 2020-07-18 09:14:06 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 106 ms / 2,000 ms |
コード長 | 824 bytes |
コンパイル時間 | 2,131 ms |
コンパイル使用メモリ | 198,596 KB |
最終ジャッジ日時 | 2025-01-12 00:09:48 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
ソースコード
#include <bits/stdc++.h> using namespace std; class UnionFind { private: int sz; vector<int> par, siz; public: UnionFind() {} UnionFind(int n) : sz(n), par(sz), siz(sz, 1) { iota(par.begin(), par.end(), 0); } int root(int x) { if (par.at(x) == x) return x; else return par.at(x) = root(par.at(x)); } void unit(int x,int y) { x = root(x), y = root(y); if (x == y) return; if (siz.at(x) < siz.at(y)) swap(x, y); par.at(y) = x; siz.at(x) += siz.at(y); } int size(int x) { x = root(x); return siz.at(x); } bool same(int x, int y) { return root(x) == root(y); } }; int main() { int N, P; cin >> N >> P; UnionFind UF(N + 1); for (int i = 2; i <= N; i++) { for (int j = i + i; j <= N; j += i) UF.unit(j - i, j); } cout << UF.size(P) << "\n"; }