結果

問題 No.2072 Anatomy
ユーザー kenskens
提出日時 2022-09-16 21:59:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 1,248 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 203,908 KB
実行使用メモリ 8,008 KB
最終ジャッジ日時 2023-08-23 15:28:51
合計ジャッジ時間 5,695 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 102 ms
7,000 KB
testcase_09 AC 108 ms
7,404 KB
testcase_10 AC 103 ms
6,532 KB
testcase_11 AC 106 ms
7,244 KB
testcase_12 AC 82 ms
6,196 KB
testcase_13 AC 111 ms
7,604 KB
testcase_14 AC 70 ms
5,724 KB
testcase_15 AC 68 ms
6,004 KB
testcase_16 AC 113 ms
7,508 KB
testcase_17 AC 106 ms
7,572 KB
testcase_18 AC 56 ms
5,552 KB
testcase_19 AC 116 ms
7,748 KB
testcase_20 AC 119 ms
7,776 KB
testcase_21 AC 113 ms
7,724 KB
testcase_22 AC 114 ms
7,932 KB
testcase_23 AC 113 ms
7,724 KB
testcase_24 AC 109 ms
8,008 KB
testcase_25 AC 117 ms
7,792 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 114 ms
7,900 KB
testcase_28 AC 112 ms
7,852 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