結果

問題 No.2072 Anatomy
ユーザー NAMIDAIRONAMIDAIRO
提出日時 2022-09-16 21:57:29
言語 C++14
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 1,326 bytes
コンパイル時間 804 ms
使用メモリ 6,952 KB
最終ジャッジ日時 2023-01-11 06:49:55
合計ジャッジ時間 4,519 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
6,948 KB
testcase_01 AC 2 ms
4,900 KB
testcase_02 AC 1 ms
6,952 KB
testcase_03 AC 2 ms
4,904 KB
testcase_04 AC 2 ms
4,900 KB
testcase_05 AC 1 ms
6,948 KB
testcase_06 AC 2 ms
6,948 KB
testcase_07 AC 2 ms
4,904 KB
testcase_08 AC 97 ms
5,732 KB
testcase_09 AC 103 ms
6,948 KB
testcase_10 AC 102 ms
5,404 KB
testcase_11 AC 105 ms
5,988 KB
testcase_12 AC 83 ms
6,948 KB
testcase_13 AC 108 ms
6,012 KB
testcase_14 AC 70 ms
5,028 KB
testcase_15 AC 65 ms
4,952 KB
testcase_16 AC 109 ms
6,280 KB
testcase_17 AC 106 ms
6,008 KB
testcase_18 AC 56 ms
6,948 KB
testcase_19 AC 112 ms
6,272 KB
testcase_20 AC 112 ms
6,348 KB
testcase_21 AC 108 ms
6,344 KB
testcase_22 AC 111 ms
6,312 KB
testcase_23 AC 110 ms
6,260 KB
testcase_24 AC 107 ms
6,948 KB
testcase_25 AC 110 ms
6,268 KB
testcase_26 AC 2 ms
4,904 KB
testcase_27 AC 109 ms
6,276 KB
testcase_28 AC 109 ms
6,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <random>
#include <iomanip>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)

template<class T>
using vi = vector<T>;

template<class T>
using vii = vector<vi<T>>;

template<class T>
using viii = vector<vii<T>>;


struct unionfind {
    vector<int> d;
    vector<int> cnt;
    unionfind(int n = 0) : cnt(n) {
        d = vector<int>(n, -1);
    }

    int find(int x) {
        if (d[x] < 0) return x;
        return d[x] = find(d[x]);
    }

    bool unite(int x, int y) {
        int rx = find(x);
        int ry = find(y);
        if (rx == ry) {
            cnt[rx]++;
            return false;
        }
        if (d[rx] > d[ry]) swap(rx, ry);
        d[rx] += d[ry];
        d[ry] = rx;
        cnt[rx] = max(cnt[rx], cnt[ry]) + 1;
        return true;
    }

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

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



int main()
{
    int n, m;
    cin >> n >> m;
    
    unionfind uf(n);
    vi<int> u(m), v(m);
    rep(i, m) cin >> u[i] >> v[i];
    rep(i, m) u[i]--, v[i]--;

    for (int i = m - 1; i >= 0; i--) uf.unite(u[i], v[i]);
    int rt = uf.find(0);
    cout << uf.cnt[rt] << endl;
    return 0;
}
0