結果

問題 No.2072 Anatomy
ユーザー ruthenruthen
提出日時 2022-09-16 22:05:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 1,477 bytes
コンパイル時間 1,959 ms
コンパイル使用メモリ 201,740 KB
実行使用メモリ 6,300 KB
最終ジャッジ日時 2023-08-23 15:39:28
合計ジャッジ時間 4,555 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 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 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 39 ms
5,720 KB
testcase_09 AC 38 ms
5,896 KB
testcase_10 AC 39 ms
5,448 KB
testcase_11 AC 39 ms
5,964 KB
testcase_12 AC 31 ms
5,336 KB
testcase_13 AC 42 ms
6,208 KB
testcase_14 AC 28 ms
5,108 KB
testcase_15 AC 24 ms
4,840 KB
testcase_16 AC 43 ms
6,236 KB
testcase_17 AC 39 ms
6,068 KB
testcase_18 AC 21 ms
4,556 KB
testcase_19 AC 42 ms
6,232 KB
testcase_20 AC 42 ms
6,232 KB
testcase_21 AC 40 ms
6,300 KB
testcase_22 AC 43 ms
6,236 KB
testcase_23 AC 41 ms
6,180 KB
testcase_24 AC 39 ms
6,176 KB
testcase_25 AC 42 ms
6,196 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 41 ms
6,156 KB
testcase_28 AC 40 ms
6,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;


struct unionfind {
    std::vector<int> parents;

    unionfind() {}
    unionfind(int n) : parents(n, -1) {}

    int leader(int x) { return parents[x] < 0 ? x : parents[x] = leader(parents[x]); }

    bool merge(int x, int y) {
        x = leader(x), y = leader(y);
        if (x == y) return false;
        if (parents[x] > parents[y]) std::swap(x, y);
        parents[x] += parents[y];
        parents[y] = x;
        return true;
    }

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

    int size(int x) { return -parents[leader(x)]; }

    void init(int n) { parents.assign(n, -1); }  // reset
};

/**
 * @brief UnionFind
 * @docs docs/data_structure/unionfind.md
 */

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    V<int> A(M), B(M);
    rep(i, M) {
        cin >> A[i] >> B[i];
        A[i]--, B[i]--;
    }
    reverse(A.begin(), A.end());
    reverse(B.begin(), B.end());
    V<int> t(N);
    unionfind uf(N);
    rep(i, M) {
        int x = uf.leader(A[i]), y = uf.leader(B[i]);
        int r = max(t[x], t[y]);
        t[x] = t[y] = r + 1;
        uf.merge(A[i], B[i]);
    }
    int ans = *max_element(t.begin(), t.end());
    cout << ans << '\n';
    return 0;
}
0