結果

問題 No.2072 Anatomy
ユーザー shoshoshomshoshoshom
提出日時 2022-09-16 23:27:29
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 1,144 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 205,504 KB
実行使用メモリ 7,056 KB
最終ジャッジ日時 2023-08-23 16:45:59
合計ジャッジ時間 4,549 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 40 ms
6,420 KB
testcase_09 AC 41 ms
6,748 KB
testcase_10 AC 41 ms
6,176 KB
testcase_11 AC 43 ms
6,548 KB
testcase_12 AC 33 ms
5,756 KB
testcase_13 AC 45 ms
6,736 KB
testcase_14 AC 29 ms
5,208 KB
testcase_15 AC 26 ms
5,360 KB
testcase_16 AC 46 ms
6,740 KB
testcase_17 AC 43 ms
6,708 KB
testcase_18 AC 23 ms
5,072 KB
testcase_19 AC 46 ms
6,944 KB
testcase_20 AC 46 ms
7,056 KB
testcase_21 AC 43 ms
6,960 KB
testcase_22 AC 47 ms
6,924 KB
testcase_23 AC 45 ms
6,996 KB
testcase_24 AC 42 ms
6,904 KB
testcase_25 AC 46 ms
6,924 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 45 ms
6,976 KB
testcase_28 AC 41 ms
6,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

class dsu {
  vector<int> p;
  vector<int> sz;

public:
  dsu(int n) : p(n), sz(n, 1) { iota(p.begin(), p.end(), 0); }

  inline int get(int x) {
    if (x == p[x]) {
      return x;
    }
    return p[x] = get(p[x]);
  }

  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      if (sz[x] < sz[y]) {
        swap(x, y);
      }
      p[y] = x;
      sz[x] += sz[y];
      return true;
    }
    return false;
  }

  inline int size(int x) {
    x = get(x);
    return sz[x];
  }
};

int main() {
  iostream::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> A(m);
  for (auto &p : A) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    p = {x, y};
  }
  vector<int> cnt(n);

  reverse(A.begin(), A.end());

  dsu uf(n + 1);
  for (auto [x, y] : A) {
    int px = uf.get(x), py = uf.get(y);
    uf.unite(x, y);
    if (px == py) {
      cnt[uf.get(x)] = cnt[px] + 1;
    } else {
      cnt[uf.get(x)] = max(cnt[px], cnt[py]) + 1;
    }
  }

  cout << *max_element(cnt.begin(), cnt.end()) << '\n';

  return 0;
}
0