結果
問題 | No.2072 Anatomy |
ユーザー |
![]() |
提出日時 | 2022-09-17 09:07:26 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 134 ms / 2,000 ms |
コード長 | 2,191 bytes |
コンパイル時間 | 1,251 ms |
コンパイル使用メモリ | 97,912 KB |
実行使用メモリ | 6,400 KB |
最終ジャッジ日時 | 2024-12-22 00:25:09 |
合計ジャッジ時間 | 4,876 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 27 |
ソースコード
#include <iostream> #include <vector> #include <map> #include <algorithm> #include <set> #include <stack> #include <queue> #include <cmath> #include <iomanip> #include <numeric> #include <string> #include <bitset> #include <assert.h> // #define _GLIBCXX_DEBUG // #define _LIBCPP_DEBUG 0 #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define revrep(i, n) for(int i = (int)(n -1); i >= 0; --i) #define range(i, s, t) for (int i = (int)(s); i < (int)(t); ++i) #define revrange(i, s, t) for(int i = (int)(t - 1); i >= (int)(s); --i) #define srange(i, s, t, step) for(int i = (int)(s); i < (int)(t); i+=int(step)) #define popcnt(x) __builtin_popcountll(x) using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<long long, long long>; template<class T> inline bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; } inline bool inside(int x, int y, int h, int w) { return (x >= 0 && x < h && y >= 0 && y < w); } int dx[] = {0, -1, 0, 1}; int dy[] = {-1, 0, 1, 0}; struct union_find{ public: union_find(int size): _n(size), root(size, -1) {} int find(int x){ if (root[x] < 0) return x; root[x] = find(root[x]); return root[x]; } bool same(int x, int y){ return find(x) == find(y); } bool merge(int x, int y){ x = find(x); y = find(y); if (x == y) return false; if (-root[x] < -root[y]) swap(x, y); root[x] += root[y]; root[y] = x; return true; } int size(int x){ return -root[x]; } private: int _n; vector<int> root; }; int main() { int n, m; cin >> n >> m; vector<int> a(m), b(m); rep(i, m) { cin >> a[i] >> b[i]; a[i]--; b[i]--; } union_find uf(n); vector<int> cnt(n, 0); revrep(i, m) { int p = uf.find(a[i]); int q = uf.find(b[i]); int x = max(cnt[p], cnt[q]); uf.merge(p, q); cnt[uf.find(p)] = x + 1; } int ans = 0; rep(i, n) chmax(ans, cnt[i]); cout << ans << endl; return 0; }