結果

問題 No.2072 Anatomy
ユーザー kenskens
提出日時 2022-09-16 21:59:40
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 1,248 bytes
コンパイル時間 4,296 ms
使用メモリ 7,932 KB
最終ジャッジ日時 2023-01-11 06:54:14
合計ジャッジ時間 7,474 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
3,376 KB
testcase_01 AC 2 ms
3,448 KB
testcase_02 AC 2 ms
3,484 KB
testcase_03 AC 2 ms
3,528 KB
testcase_04 AC 2 ms
3,452 KB
testcase_05 AC 2 ms
3,380 KB
testcase_06 AC 2 ms
3,384 KB
testcase_07 AC 2 ms
3,528 KB
testcase_08 AC 102 ms
6,996 KB
testcase_09 AC 109 ms
7,332 KB
testcase_10 AC 106 ms
6,220 KB
testcase_11 AC 108 ms
7,276 KB
testcase_12 AC 85 ms
6,056 KB
testcase_13 AC 114 ms
7,616 KB
testcase_14 AC 72 ms
5,728 KB
testcase_15 AC 68 ms
6,020 KB
testcase_16 AC 115 ms
7,600 KB
testcase_17 AC 111 ms
7,532 KB
testcase_18 AC 57 ms
5,424 KB
testcase_19 AC 117 ms
7,796 KB
testcase_20 AC 122 ms
7,860 KB
testcase_21 AC 115 ms
7,800 KB
testcase_22 AC 117 ms
7,868 KB
testcase_23 AC 115 ms
7,868 KB
testcase_24 AC 112 ms
7,932 KB
testcase_25 AC 117 ms
7,800 KB
testcase_26 AC 1 ms
3,440 KB
testcase_27 AC 115 ms
7,864 KB
testcase_28 AC 115 ms
7,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;

class Union_Find{
  vector<int> par; vector<int> rank; vector<int> num;
public:
  Union_Find(int n) {
    par = vector<int>(n); rank = vector<int>(n,0); num = vector<int>(n,1);
    rep(i,n) par[i] = i;
  }
  int find(int x) {
    if(par[x] == x) return x;
    else  return par[x] = find(par[x]);
  }
  int number(int x) {
    return num[find(x)];
  }
  void unite(int x, int y) {
    x = find(x); y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) swap(x, y);
    if (rank[x] == rank[y]) rank[x]++;
    par[y] = x;
    num[x] += num[y];
  } 
  bool same(int x, int y) {
    return find(x) == find(y);
  }
};

int main(){
  int n, m;
  cin >> n >> m;
  vector<int> u(m), v(m);
  rep(i,m) {
    cin >> u[i] >> v[i];
    u[i]--; v[i]--;
  }
  vector<int> dp(n);
  Union_Find uf(n);
  for(int i = m-1; i >= 0; i--) {
    if(uf.same(u[i],v[i])) {
      dp[uf.find(u[i])] = dp[uf.find(u[i])] + 1;
    } else {
      int cur = max(dp[uf.find(u[i])],dp[uf.find(v[i])]);
      uf.unite(u[i],v[i]);
      dp[uf.find(u[i])] = cur+1;
    }
  }
  int ans = 0;
  rep(i,n) ans = max(ans,dp[i]);
  cout << ans << endl;
  return 0;
}
0