結果
問題 | No.2664 Prime Sum |
ユーザー | elphe |
提出日時 | 2024-07-04 20:27:31 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 2,319 bytes |
コンパイル時間 | 892 ms |
コンパイル使用メモリ | 79,936 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-04 20:27:35 |
合計ジャッジ時間 | 1,928 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 37 |
ソースコード
#include <iostream>#include <vector>using namespace std;class BipartiteUnionFind{protected:vector<size_t> root_of, size_of, unconnectables_of;vector<bool> color_of;size_t group;virtual size_t retrieve_true_root_of(const size_t index) { return root_of[index] == index ? index : retrieve_true_root_of(root_of[index]),color_of[index] = color_of[index] ^ color_of[root_of[index]], root_of[index] = root_of[root_of[index]]; }public:BipartiteUnionFind(const size_t N) : root_of(N), size_of(N, 1), unconnectables_of(N, 1), color_of(N, false), group(N) { for (size_t i = 0; i != N;++i) root_of[i] = i; }virtual bool is_connected(const size_t a, const size_t b) { return retrieve_true_root_of(a) == retrieve_true_root_of(b); }virtual bool is_connectable(const size_t a, const size_t b) { return color_of[a] != color_of[b]; }virtual bool merge(const size_t a, const size_t b){if (retrieve_true_root_of(a) != retrieve_true_root_of(b)){unconnectables_of[root_of[a]] += color_of[a] ? count_unconnectables_of(b) : size_of[root_of[b]] - count_unconnectables_of(b);color_of[root_of[b]] = !(color_of[a] ^ color_of[b]), color_of[b] = !color_of[a];size_of[root_of[a]] += size_of[root_of[b]];root_of[b] = root_of[root_of[b]] = root_of[a];--group;return true;}elsereturn false;}size_t size_of_group_of(const size_t index) { return size_of[retrieve_true_root_of(index)]; }size_t groups() { return group; }size_t count_unconnectables_of(const size_t index) { return retrieve_true_root_of(index), color_of[index] ? size_of[root_of[index]] -unconnectables_of[root_of[index]] : unconnectables_of[root_of[index]]; }};int main(){cin.tie(nullptr);ios::sync_with_stdio(false);int n, m, a, b, i;cin >> n >> m;BipartiteUnionFind buf(n);for (i = 0; i != m; ++i){cin >> a >> b, --a, --b;if (buf.is_connected(a, b)){if (!buf.is_connectable(a, b)){for (++i; i != m; ++i) cin >> a >> b;cout << "No\n";return 0;}}else buf.merge(a, b);}cout << "Yes\n";return 0;}