結果

問題 No.2072 Anatomy
ユーザー NAMIDAIRONAMIDAIRO
提出日時 2022-09-16 21:57:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,326 bytes
コンパイル時間 960 ms
コンパイル使用メモリ 102,652 KB
実行使用メモリ 6,464 KB
最終ジャッジ日時 2023-08-23 15:26:07
合計ジャッジ時間 4,902 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 98 ms
5,668 KB
testcase_09 AC 107 ms
6,008 KB
testcase_10 AC 102 ms
5,488 KB
testcase_11 AC 104 ms
5,884 KB
testcase_12 AC 82 ms
5,256 KB
testcase_13 AC 109 ms
5,940 KB
testcase_14 AC 72 ms
4,920 KB
testcase_15 AC 65 ms
4,832 KB
testcase_16 AC 110 ms
6,200 KB
testcase_17 AC 109 ms
5,936 KB
testcase_18 AC 58 ms
4,660 KB
testcase_19 AC 111 ms
6,192 KB
testcase_20 AC 114 ms
6,212 KB
testcase_21 AC 112 ms
6,196 KB
testcase_22 AC 114 ms
6,144 KB
testcase_23 AC 111 ms
6,452 KB
testcase_24 AC 108 ms
6,272 KB
testcase_25 AC 109 ms
6,464 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 109 ms
6,284 KB
testcase_28 AC 110 ms
6,200 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