結果

問題 No.2072 Anatomy
ユーザー umimelumimel
提出日時 2022-09-16 21:56:19
言語 C++14
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 1,522 bytes
コンパイル時間 1,403 ms
使用メモリ 11,024 KB
最終ジャッジ日時 2023-01-11 06:44:54
合計ジャッジ時間 5,130 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
6,952 KB
testcase_01 AC 2 ms
6,952 KB
testcase_02 AC 2 ms
4,900 KB
testcase_03 AC 1 ms
4,904 KB
testcase_04 AC 2 ms
6,952 KB
testcase_05 AC 1 ms
4,900 KB
testcase_06 AC 1 ms
4,900 KB
testcase_07 AC 1 ms
6,948 KB
testcase_08 AC 101 ms
9,576 KB
testcase_09 AC 108 ms
10,312 KB
testcase_10 AC 105 ms
8,876 KB
testcase_11 AC 109 ms
9,972 KB
testcase_12 AC 85 ms
8,120 KB
testcase_13 AC 113 ms
10,708 KB
testcase_14 AC 72 ms
7,592 KB
testcase_15 AC 68 ms
7,800 KB
testcase_16 AC 115 ms
10,696 KB
testcase_17 AC 111 ms
10,352 KB
testcase_18 AC 58 ms
7,132 KB
testcase_19 AC 116 ms
10,992 KB
testcase_20 AC 116 ms
10,948 KB
testcase_21 AC 113 ms
10,920 KB
testcase_22 AC 116 ms
10,960 KB
testcase_23 AC 114 ms
10,920 KB
testcase_24 AC 112 ms
10,940 KB
testcase_25 AC 116 ms
10,960 KB
testcase_26 AC 1 ms
4,900 KB
testcase_27 AC 114 ms
10,936 KB
testcase_28 AC 113 ms
11,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

struct UnionFind{
    vector<ll> parent;
    vector<ll> sizes;
    vector<ll> cnt;
    
    UnionFind(ll N) : parent(N), sizes(N, 1), cnt(N, 0){
        rep(i, N) parent[i] = i;
    }
    
    ll root(ll x){
        if (parent[x] == x) return x;
        return parent[x] = root(parent[x]);
    }
 
    void unite(ll x, ll y){
        ll rx = root(x);
        ll ry = root(y);
        if (rx == ry){
            cnt[rx]++;
            return;
        }
        if (sizes[rx] < sizes[ry]) swap(rx, ry);
        cnt[rx] = max(cnt[rx], cnt[ry])+1;
        sizes[rx] += sizes[ry];
        parent[ry] = rx;
    }
 
    bool same(ll x, ll y){
        ll rx = root(x);
        ll ry = root(y);
        return rx == ry;
    }
 
    ll size(ll x){
        return sizes[root(x)];
    }

    ll count(ll x){
        return cnt[root(x)];
    }
};

int main(){
    ll n, m;
    cin >> n >> m;

    vector<pll> e(m);
    rep(i, m){
        ll u, v;
        cin >> u >> v;
        u--; v--;
        e[i] = {u, v};
    }

    UnionFind UF(n);
    
    for(ll i=m-1; i>=0; i--){
        UF.unite(e[i].fi, e[i].se);
    }

    cout << UF.count(0) << endl;
}
0