結果
問題 |
No.1390 Get together
|
ユーザー |
![]() |
提出日時 | 2022-09-04 13:21:24 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 316 ms / 2,000 ms |
コード長 | 1,578 bytes |
コンパイル時間 | 722 ms |
コンパイル使用メモリ | 83,116 KB |
最終ジャッジ日時 | 2025-02-07 02:36:10 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#include <iostream> #include <vector> #include <deque> #include <numeric> class UnionFind { std::vector<int> m_root; std::vector<int> m_depth; std::vector<int> m_size; public: UnionFind(int size) : m_root(size), m_depth(size, 0), m_size(size, 1) { std::iota(m_root.begin(), m_root.end(), 0); } void unite(int x, int y) { x = root(x); y = root(y); if(x == y) { return; } auto t = size(x) + size(y); m_size[x] = m_size[y] = t; if(m_depth[x] < m_depth[y]) { m_root[x] = y; } else { m_root[y] = x; } if(m_depth[x] == m_depth[y]) { ++m_depth[x]; } } bool isSame(int x, int y) { return root(x) == root(y); } int root(int x) { if(m_root[x] == x) { return x; } return m_root[x] = root(m_root[x]); } int size(int x) { if(m_root[x] == x) { return m_size[x]; } return size(m_root[x] = root(m_root[x])); } }; using ll = long long; using std::cout; using std::cin; constexpr char endl = '\n'; signed main() { ll n, m; cin >> n >> m; std::vector<std::deque<ll>> cv(n); for(int _ = 0; _ < n; ++_) { ll b, c; cin >> b >> c; cv[c - 1].emplace_back(b - 1); } auto dsu = UnionFind(m); ll ans = 0; for(const auto& dq : cv) if(!dq.empty()) { auto base = dq.front(); for(const auto& tg : dq) { if(!dsu.isSame(base, tg)) { dsu.unite(base, tg); ++ans; } } } cout << ans << endl; }