結果

問題 No.2072 Anatomy
ユーザー simansiman
提出日時 2022-09-17 03:03:39
言語 C++17(clang)
(14.0.0 + boost 1.83.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 1,562 bytes
コンパイル時間 3,540 ms
コンパイル使用メモリ 103,132 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2023-08-23 17:24:02
合計ジャッジ時間 4,832 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 107 ms
7,392 KB
testcase_09 AC 116 ms
7,504 KB
testcase_10 AC 113 ms
6,516 KB
testcase_11 AC 119 ms
7,292 KB
testcase_12 AC 90 ms
6,204 KB
testcase_13 AC 124 ms
7,776 KB
testcase_14 AC 76 ms
5,668 KB
testcase_15 AC 73 ms
6,048 KB
testcase_16 AC 123 ms
7,744 KB
testcase_17 AC 120 ms
7,756 KB
testcase_18 AC 64 ms
5,728 KB
testcase_19 AC 129 ms
7,756 KB
testcase_20 AC 125 ms
7,760 KB
testcase_21 AC 120 ms
7,836 KB
testcase_22 AC 129 ms
7,764 KB
testcase_23 AC 126 ms
7,808 KB
testcase_24 AC 119 ms
7,756 KB
testcase_25 AC 124 ms
7,844 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 124 ms
7,824 KB
testcase_28 AC 121 ms
7,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_N = 200000;
vector<int> _parent;
vector<int> _rank;
vector<int> _size;

class UnionFind {
public:
  UnionFind(int n) {
    for (int i = 0; i < n; ++i) {
      _parent.push_back(i);
      _rank.push_back(0);
      _size.push_back(1);
    }
  }

  int find(int x) {
    if (_parent[x] == x) {
      return x;
    } else {
      return _parent[x] = find(_parent[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;

    if (_rank[x] < _rank[y]) {
      _parent[x] = y;
      _size[y] += _size[x];
    } else {
      _parent[y] = x;
      _size[x] += _size[y];
      if (_rank[x] == _rank[y]) ++_rank[x];
    }
  }

  bool same(int x, int y) {
    return find(x) == find(y);
  }

  int size(int x) {
    return _size[find(x)];
  }
};

int main() {
  int N, M;
  cin >> N >> M;

  UnionFind uf(N + 1);
  vector<int> counter(N + 1, 0);
  int U[M];
  int V[M];
  for (int i = 0; i < M; ++i) {
    cin >> U[i] >> V[i];
  }

  for (int i = M - 1; i >= 0; --i) {
    int u = U[i];
    int v = V[i];
    int p1 = uf.find(u);
    int p2 = uf.find(v);

    if (uf.same(u, v)) {
      counter[p1]++;
    } else {
      uf.unite(u, v);
      int p = uf.find(u);
      counter[p] = max(counter[p1], counter[p2]) + 1;
    }
  }

  cout << counter[uf.find(1)] << endl;

  return 0;
}

0